本文整理汇总了C++中vd::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ vd::begin方法的具体用法?C++ vd::begin怎么用?C++ vd::begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vd
的用法示例。
在下文中一共展示了vd::begin方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Sum
double Sum( vd v)
{
vd::iterator it;
double sum = 0.0;
for(it = v.begin(); it < v.end(); ++it)
sum += *it;
return(sum);
}
示例2: SumSquaredDifferences
double SumSquaredDifferences( vd v, double avg)
{
vd::iterator it;
double sumSq = 0.0;
for(it = v.begin(); it < v.end(); ++it)
sumSq += pow(*it - avg,2.0);
return( sumSq );
}
示例3: average
double average( vd v)
{
vd::iterator it;
double sum = 0.0;
if ( v.size() == 0) return(0.0);
for(it = v.begin(); it < v.end(); ++it)
sum += *it;
return(sum/v.size());
}
示例4: BellmanFordMoore
//shortest path from a node n to every other node:
//this is for any graphs even ones that have negative weights -- can detect negative cycles
//for every vertex, relax E times
//as an optimization we use a queue to store vertices that changed
//and relax paths only from that queue
//to detect negative loops can check if node n gets updated on any the n-th iteration
int BellmanFordMoore(const graphtp & g, vd & D, int src)
{
int comparisons = 0;
vi visited(g.size());
std::vector<edge> P(g.size()); //parent node for node e.to is e.from
for(vd::iterator it = D.begin(); it != D.end(); ++it)
*it = INFINITY;
D[src] = 0;
si lastupdated;
//keep a queue (set) of vertices (nodes) that were updated last time around
//do the relaxation V times
for(size_t i = 0; i < g.size(); i++) {
lastupdated.insert(i);
}
//without optimization loop would be: for(int i = 0; i < g.size(); i++)
size_t counter = 0;
while(!lastupdated.empty()) {
counter++;
si newlast;
//visit and relax all edges (two for loopxs needed to visit all edges)
for(si::iterator jt = lastupdated.begin();jt != lastupdated.end(); jt++)
{
int v = *jt;
out("next to work on is %d\n", v);
for(ve::const_iterator it = g[v].begin(); it != g[v].end(); ++it)
{
float dist = D[v] + it->weight;
assert(v == it->from && "not the same node");
if(D[it->to] > dist) { //this is the relaxation step
out("counter %d, it->to %d\n", counter, it->to);
if(counter >= g.size()) {
//we should not update a node in a stage greater than sz
printf("negative cycles exists distance %d to %d is %f\n", v, it->to, dist);
//need to trace back from P[v]
//printSP(P, it->to, v);
printSP(P, v, it->to);
printf("\n");
return -1;
}
D[it->to] = dist;
P[it->to] = *it;
newlast.insert(it->to); //track which changed
out("new distance %d to %d is %f\n", v, it->to, dist);
}
}
}
lastupdated = newlast;
}
for(size_t i = 0; i < D.size(); i++) {
printf("%d -> %d sp=%.2f ", src, i, D[i]);
printSP(P, i, src);
printf("\n");
}
out("comparisons %d\n", comparisons);
return 0;
}