Difference between revisions of "CPP/STL/ForEach"
From ProgrammingExamples
< CPP
FirstPerson (Talk | contribs) (Created page with '<source lang = "cpp"> #include <iostream> #include <algorithm> #include <vector> using namespace std; void showSquaredValue(const int n){ cout << n << "*" << n << " = " …') |
Daviddoria (Talk | contribs) |
||
| Line 1: | Line 1: | ||
| + | ==ForEach.cpp== | ||
<source lang = "cpp"> | <source lang = "cpp"> | ||
| − | |||
#include <iostream> | #include <iostream> | ||
#include <algorithm> | #include <algorithm> | ||
#include <vector> | #include <vector> | ||
| − | + | struct printFunctor | |
| − | + | { | |
| − | void | + | void operator() (int x) |
| − | + | { | |
| + | std::cout << x; | ||
| + | } | ||
| + | |||
}; | }; | ||
| − | + | int main() | |
| − | + | { | |
| − | + | std::vector<int> v; | |
| − | + | v.push_back(1); | |
| − | + | v.push_back(2); | |
| − | + | v.push_back(3); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | printFunctor myPrint; | |
| − | + | std::for_each(v.begin(), v.end(), myPrint); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | return 0; | |
| − | + | } | |
| + | </source> | ||
| − | + | ==CMakeLists.txt== | |
| − | + | <source lang="cmake"> | |
| + | cmake_minimum_required(VERSION 2.6) | ||
| + | |||
| + | PROJECT(ForEach) | ||
| + | ADD_EXECUTABLE(ForEach ForEach.cpp ) | ||
| + | |||
| + | </source> | ||
Revision as of 08:19, 26 February 2011
ForEach.cpp
#include <iostream> #include <algorithm> #include <vector> struct printFunctor { void operator() (int x) { std::cout << x; } }; int main() { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); printFunctor myPrint; std::for_each(v.begin(), v.end(), myPrint); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ForEach) ADD_EXECUTABLE(ForEach ForEach.cpp )