Y
Yan
Hi,
I don't seem to be able to use for_each if it should replace a 'for'
loop in a method (constructor in my case) and inside that 'for' loop a
class member variable is being accessed. The presence of this member
variable prevents me from using a static or global method to be passed
as a third parameter to for_each, and mem_fun doesn't seem to work for
me either as I am not going to execute a method of an iterator but
pass an iterator as a parameter. I am not sure that explanation makes
a lot of sense, so below is a sample code. I would like to be able to
replace the 'for' loop inside C class constructor with a for_each.
Please don't pay attention to what is actually happening with these
two vectors of ints, it's just for illustration purposes. Thanks.
-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> vectorInts;
typedef vector<int>::iterator vectorIntsIter;
class C {
public:
C(pair<vectorIntsIter, vectorIntsIter> aPair) {
for (vectorIntsIter iter = aPair.first; iter != aPair.second; +
+iter) {
// do something that involves member variable
// which seems to prevent using a static method
// such as for example this:
ints.push_back(*iter + 10);
}
}
void printAllElements() {
// here a static method 'print' is successfully used
for_each(ints.begin(), ints.end(), print);
}
private:
static void print(int number) {
cout << number << endl;
}
private:
vectorInts ints;
};
int main() {
vectorInts ints;
ints.push_back(0);
ints.push_back(1);
ints.push_back(2);
ints.push_back(3);
pair<vectorIntsIter, vectorIntsIter> aPair(ints.begin(),
ints.end());
C c(aPair);
c.printAllElements();
return 0;
}
I don't seem to be able to use for_each if it should replace a 'for'
loop in a method (constructor in my case) and inside that 'for' loop a
class member variable is being accessed. The presence of this member
variable prevents me from using a static or global method to be passed
as a third parameter to for_each, and mem_fun doesn't seem to work for
me either as I am not going to execute a method of an iterator but
pass an iterator as a parameter. I am not sure that explanation makes
a lot of sense, so below is a sample code. I would like to be able to
replace the 'for' loop inside C class constructor with a for_each.
Please don't pay attention to what is actually happening with these
two vectors of ints, it's just for illustration purposes. Thanks.
-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> vectorInts;
typedef vector<int>::iterator vectorIntsIter;
class C {
public:
C(pair<vectorIntsIter, vectorIntsIter> aPair) {
for (vectorIntsIter iter = aPair.first; iter != aPair.second; +
+iter) {
// do something that involves member variable
// which seems to prevent using a static method
// such as for example this:
ints.push_back(*iter + 10);
}
}
void printAllElements() {
// here a static method 'print' is successfully used
for_each(ints.begin(), ints.end(), print);
}
private:
static void print(int number) {
cout << number << endl;
}
private:
vectorInts ints;
};
int main() {
vectorInts ints;
ints.push_back(0);
ints.push_back(1);
ints.push_back(2);
ints.push_back(3);
pair<vectorIntsIter, vectorIntsIter> aPair(ints.begin(),
ints.end());
C c(aPair);
c.printAllElements();
return 0;
}