stl - A question about for_each in vector of c++ -


i have question for_each in vector, code following:

#include <iostream> #include <algorithm> #include <vector> using namespace std;   struct myclass {   void operator() (int i) {cout << " " << i;} } myobject;  int main () {   vector<int> myvector(3,4);    cout << "\nmyvector contains:";   for_each (myvector.begin(), myvector.end(), myobject);    cout << endl;    return 0; } 

should third argument of for_each() function name? if pass name of struct, how works?

this functor.

std::for_each function template expands this:

for (iter = myvector.begin(); iter != myvector.end(); ++iter) {     myobject(*iter); } 

so myobject can either function pointer, or can object overload operator().


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -