[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppForEach
There are two kinds of examples below. The first uses std::for_each combined with simple functions.
The second piece of code shows the use of functors for more advanced functionality. It is advised to use the latter [3], but I will show the first as an example. There are also STL functors.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppForEach
(C++) std::for_each
Algorithm to perform a function on a sequence of values (e.g. a vector. Prefer algorithms over hand-written loops [1,2].There are two kinds of examples below. The first uses std::for_each combined with simple functions.
The second piece of code shows the use of functors for more advanced functionality. It is advised to use the latter [3], but I will show the first as an example. There are also STL functors.
Example: Use of plain functions
void setToOne(int& i) { i = 1; } void multiplyByTwo(int& i) { i*=2; } void setToRandom(int& i) { i = std::rand()%10; } void coutVector(const std::vector<int>& myVector) { const int size = myVector.size(); for (int i=0; i!=size; ++i) { std::cout << i << " : " << myVector[i] << std::endl; } } int main() { const int size = 5; std::vector<int> myVector(size); std::for_each(myVector.begin(),myVector.end(), setToOne); coutVector(myVector); std::for_each(myVector.begin(),myVector.end(), multiplyByTwo); coutVector(myVector); std::for_each(myVector.begin(),myVector.end(), setToRandom); coutVector(myVector); std::for_each(myVector.begin(),myVector.end(), multiplyByTwo); coutVector(myVector); return 0; }
- include <iostream>
- include <vector>
- include <algorith>
Example: Use of functors
The preffered way [3].class MyInitializer { public: MyInitializer() : index(0) {} template<class T> void operator () (T & a) { a = index; ++index; } private: int index; }; class MyIndexCout { public: MyIndexCout() : index(0) {} template<class T> void operator () (const T & a) { std::cout << index << " : " << a << std::endl; ++index; } private: int index; }; class MySquarer { public: MySquarer() {} template<class T> void operator () (T & a) { a*=a; } }; int main() { const int size = 10; std::vector<int> myVector(size); std::for_each(myVector.begin(), myVector.end(), MyInitializer()); std::for_each(myVector.begin(), myVector.end(), MyIndexCout()); std::cout << "-----------" << std::endl; std::for_each(myVector.begin(), myVector.end(), MySquarer()); std::for_each(myVector.begin(), myVector.end(), MyIndexCout()); }
- include <iostream>
- include <vector>
- include <algorith>
References
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4 18.12.1 : 'Prefer algorithms over loops'.
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 84: 'Prefer algorithm calls to handwritten loops.'
- 3) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 88: 'Prefer function objects over functions as algorithm and comparer arguments.'
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
