stl 学习笔记 8
2007-11-28 00:23
323 查看
function object 仿函数
//stl/ foreach2.cpp
/**//*
using function object
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**//*
function object(仿函数,函数对象)
*/
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());
cout<<endl;
}
// result
/**//*
1 2 3 4 5 6 7 8 9
*/
//stl/add1.cpp
/**//*
using function object
*/
#include <iostream>
#include <list>
#include <algorithm>
#include "print.h"
using namespace std;
/**//*
function object AddValue
*/
class AddValue...{
public:
AddValue(int v): theValue(v)
...{}
void operator() (int & elem) const
...{
elem += theValue;
}
private:
int theValue;
};
int main()
...{
list<int> coll;
for(int i = 1; i<=9; ++i)
...{
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: ");
for_each(coll.begin(),coll.end(),AddValue(10));
PRINT_ELEMENTS(coll,"after adding 10:");
for_each(coll.begin(),coll.end(),AddValue(*coll.begin()));
PRINT_ELEMENTS(coll,"after adding first element:");
return 1;
}
// result
/**//*
initialized: 1 2 3 4 5 6 7 8 9
after adding 10:11 12 13 14 15 16 17 18 19
after adding first element:22 23 24 25 26 27 28 29 30
*/
//stl/ foreach2.cpp
/**//*
using function object
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**//*
function object(仿函数,函数对象)
*/
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());
cout<<endl;
}
// result
/**//*
1 2 3 4 5 6 7 8 9
*/
//stl/add1.cpp
/**//*
using function object
*/
#include <iostream>
#include <list>
#include <algorithm>
#include "print.h"
using namespace std;
/**//*
function object AddValue
*/
class AddValue...{
public:
AddValue(int v): theValue(v)
...{}
void operator() (int & elem) const
...{
elem += theValue;
}
private:
int theValue;
};
int main()
...{
list<int> coll;
for(int i = 1; i<=9; ++i)
...{
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: ");
for_each(coll.begin(),coll.end(),AddValue(10));
PRINT_ELEMENTS(coll,"after adding 10:");
for_each(coll.begin(),coll.end(),AddValue(*coll.begin()));
PRINT_ELEMENTS(coll,"after adding first element:");
return 1;
}
// result
/**//*
initialized: 1 2 3 4 5 6 7 8 9
after adding 10:11 12 13 14 15 16 17 18 19
after adding first element:22 23 24 25 26 27 28 29 30
*/
相关文章推荐
- C++学习笔记——STL(二)vector容器
- C++ STL 学习笔记 set
- boolan——c++学习笔记之STL迭代器分类
- PKU C++程序设计实习 学习笔记6 标准模板库STL
- 【学习笔记】C++高级程序设计:STL:Set 和 Multiset
- Effective STL学习笔记-条款7
- STL学习笔记-stack容器(栈)
- STL学习笔记-容器的共性机制
- STL中的vector学习笔记
- STL学习笔记之容器--deque(一)
- STL学习笔记之容器--优先队列
- C++ STL 学习笔记 函数对象
- STL学习笔记之容器--string
- STL学习笔记
- STL学习笔记(二):STL对待错误的态度
- stl学习笔记4
- STL学习笔记之容器--list(一)
- C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
- <<C++标准程序库>>中的STL简单学习笔记