O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Traversal

We often need to traverse a container one element at a time, performing a computation on each element. We picked the STL's accumulate() function to represent container traversal. The accumulate() function traverses the container from beginning to end, adding all elements in the process. The containers under test were an array, vector, and list. Each container stored an identical collection of 10,000 random integers. The test code for the various containers is as follows:

 void vectorTraverse (vector<int> *v, int size) { int sum = accumulate(v->begin(), v->end(),0); } void arrayTraverse (int *a, int size) { int sum = accumulate(&a[0], &a[size],0); } void listTraverse (list<int> *l, int size) { int sum = accumulate(l->begin(), l->end(),0); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required