转载,由于我看到的,都是转的,他给的原帖地址失效了,我不知道该怎么写,于是在此不写原帖地址
Introduction
學習過STL的container後,想要存取每一個iterator,你一定寫過以下的程式
#include < vector >
#include < iostream >
using namespace std;
int main() {
int ia[] = {1, 2, 3};
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
for(vector::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{
cout << *iter << endl;
}
}
1
2
3
for (vector < int > ::const_iterator iter = ivec.begin(); iter != ivec.end(); ++ iter) {
template < typename InputIterator, typename Function >
Function for_each(InputIterator beg, InputIterator end, Function f) {
while(beg != end)
f(*beg++);
}
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_GlobalFunction.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with global function6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
13
using namespace std; 14
15
void printElem( int & elem) {16
cout << elem << endl;17
} 18
19
int main() {20
int ia[] = {1, 2, 3};
21
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
22
23
for_each(ivec.begin(), ivec.end(), printElem); 24
}
1
2
3
for_each(ivec.begin(), ivec.end(), printElem);
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_GlobalFunctionWithParameter.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with global function with Parameter6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
void printElem( int elem, const char * prefix) {17
cout << prefix << elem << endl;18
} 19
20
int main() {21
int ia[] = {1, 2, 3};
22
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
23
24
for_each(ivec.begin(), ivec.end(), bind2nd(ptr_fun(printElem), “Element:”)); 25
}
Element: 1
Element: 2
Element: 3
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_FunctionObject.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with function object6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
13
using namespace std; 14
15
struct printElem {16
void operator() (int elem) {17
cout << elem << endl;18
} 19
} ; 20
21
int main() {22
int ia[] = {1, 2, 3};
23
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
24
25
for_each(ivec.begin(), ivec.end(), printElem()); 26
}
1
2
3
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_FunctionObjectWithParameter.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with function object with parameter6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
13
using namespace std; 14
15
struct printElem {16
const char* _prefix;17

18
printElem(const char* prefix) : _prefix(prefix) {}19
20
void operator() (int elem) {21
cout << _prefix << elem << endl;22
} 23
} ; 24
25
int main() {26
int ia[] = {1, 2, 3};
27
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
28
29
for_each(ivec.begin(), ivec.end(), printElem(“Element:”)); 30
}
Element: 1
Element: 2
Element: 3
for_each(_doorVec.begin(), _doorVec.end(), & Door::open);
for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref( & Door::open));
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3

4
Filename : GenericAlgo_for_each_MemberFunctionObject.cpp5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++6
Description : Demo how to use for_each with member function with object7
Release : 05/11/2007 1.08
*/ 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
class Door {17
public:18
void open() const {19
cout << "open door horizontally" << endl;20
}21
22
void close() const {23
cout << "close door horizontally" << endl;24
}25
} ; 26
27
class DoorController {28
protected:29
vector_doorVec;
30
31
public: 32
void addDoor(Door aDoor) {33
_doorVec.push_back(aDoor);34
} 35
36
void openDoor() const {37
for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref(&Door::open));38
} 39
} 40
41
int main() {42
DoorController dc;43
dc.addDoor(Door());44
dc.addDoor(Door());45
dc.openDoor();46
}
open door horizontally
open door horizontally
for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref( & Door::open));
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3

4
Filename : GenericAlgo_for_each_MemberFunctionObjectPointer.cpp5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++6
Description : Demo how to use for_each with member function with object pointer7
Release : 05/11/2007 1.08
*/ 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
class AbstractDoor {17
public:18
virtual void open() const {19
cout << "open door horizontally" << endl;20
}21
22
virtual void close() const {23
cout << "close door horizontally" << endl;24
}25
} ; 26
27
class HorizontalDoor : public AbstractDoor {28
} ; 29
30
class VerticalDoor : public AbstractDoor {31
public:32
void open() const {33
cout << "open door vertically" << endl;34
}35
36
void close() const {37
cout << "close door vertically" << endl;38
}39
} ; 40
41
class DoorController {42
protected:43
vector_doorVec;
44
45
public: 46
void addDoor(AbstractDoor& aDoor) {47
_doorVec.push_back(&aDoor);48
} 49
50
void openDoor() const {51
for_each(_doorVec.begin(), _doorVec.end(), mem_fun(&AbstractDoor::open));52
} 53
} 54
55
int main() {56
DoorController dc;57
dc.addDoor(HorizontalDoor());58
dc.addDoor(VerticalDoor());59
dc.openDoor();60
}
open door horizontally
open door vertically
for_each(_doorVec.begin(), _doorVec.end(), mem_fun( & AbstractDoor::open));
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3

4
Filename : GenericAlgo_for_each_MemberFunctionObjectPointerWithParameter.cpp5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++6
Description : Demo how to use for_each with member function with object pointer7
Release : 05/11/2007 1.08
*/ 9
#include < iostream > 10
#include < vector > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
class AbstractDoor {17
public:18
virtual void open() const {19
cout << "open door horizontally" << endl;20
}21
22
virtual void close() const {23
cout << "close door horizontally" << endl;24
}25
26
virtual void openDoorBy(const char* name) const {27
cout << name << " ";28
open();29
}30
} ; 31
32
class HorizontalDoor : public AbstractDoor {33
} ; 34
35
class VerticalDoor : public AbstractDoor {36
public:37
void open() const {38
cout << "open door vertically" << endl;39
}40
41
void close() const {42
cout << "close door vertically" << endl;43
}44
} ; 45
46
class DoorController {47
protected:48
vector_doorVec;
49
50
public: 51
void addDoor(AbstractDoor& aDoor) {52
_doorVec.push_back(&aDoor);53
} 54
55
void openDoor() const {56
for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun(&AbstractDoor::openDoorBy), “John”));57
} 58
} 59
60
int main() {61
DoorController dc;62
dc.addDoor(HorizontalDoor());63
dc.addDoor(VerticalDoor());64
dc.openDoor();65
}
John open door horizontally 2
John open door vertically
for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun( & AbstractDoor::openDoorBy), ” John ” ));
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_FunctionTemplate.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with function template6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
13
using namespace std; 14
15
template < typename T > 16
void printElem(T elem) {17
cout << elem << endl;18
} 19
20
int main() {21
int ia[] = {1, 2, 3};
22
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
23
24
for_each(ivec.begin(), ivec.end(), printElem);
25
//for_each(ivec.begin(), ivec.end(), (void(*)(int))printElem); 26
}
1
2
3
for_each(ivec.begin(), ivec.end(), printElem < int > );
for_each(ivec.begin(), ivec.end(), ( void ( * )( int ))printElem);
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_FunctionTemplateWithNontypeParameter.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with function template with nontype parameter6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
13
using namespace std; 14
15
template < typename T, int i > 16
void printElem(T elem) {17
cout << i << ":" << elem << endl;18
} 19
20
int main() {21
int ia[] = {1, 2, 3};
22
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
23
24
for_each(ivec.begin(), ivec.end(), printElem);
25
}
5 : 1
5 : 2
5 : 3
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_ClassTemplate.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with class template6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
template < typename T > 17
class printElem : public unary_function < T, void > {18
public:19
void operator() (T elem) {20
cout << elem << endl;21
}22
} ; 23
24
int main() {25
int ia[] = {1, 2, 3};
26
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
27
28
for_each(ivec.begin(), ivec.end(), printElem());
29
}
1
2
3
class printElem : public unary_function < T, void > {
template < class InputIterator, class UnaryFunction >
UnaryFunction for_each(InputIterator first, InputIterator last, UnaryFunction f);
/* 2
(C) OOMusou 2007 http://oomusou.cnblogs.com3
Filename : GenericAlgo_for_each_ClassTemplateWithParameter.cpp4
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++5
Description : Demo how to use for_each with class template & parameter6
Release : 05/11/2007 1.07
*/ 8
#include < iostream > 9
#include < vector > 10
#include < iostream > 11
#include < algorithm > 12
#include < functional > 13
14
using namespace std; 15
16
template < typename T, typename U > 17
class printElem : public unary_function < T, void > {18
private:19
U _prefix;20
21
public:22
printElem(U prefix) : _prefix(prefix) {}23
24
void operator() (T elem) {25
cout << _prefix << elem << endl;26
}27
} ; 28
29
int main() {30
int ia[] = {1, 2, 3};
31
vectorivec(ia, ia + sizeof(ia) / sizeof(int));
32
33
for_each(ivec.begin(), ivec.end(), printElem(“Element:”));
34
}
Element: 1
Element: 2
Element: 3
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218877.html原文链接:https://javaforall.net
