W
webinfinite
Could anyone explain this snippet for me?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};
int main() {
vector<int> coll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);
for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}
for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?
Thank you for your response.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};
int main() {
vector<int> coll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);
for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}
for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?
Thank you for your response.