函数对象与 for_each结合使用
2012-05-10 11:06
477 查看
简单使用for_each
//程序1
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> Setstr;
void Show(string sr)
{
cout <<" "<< sr.c_str() << endl;
}
int main(int argc, char * argv[])
{
Setstr.insert("abd");
Setstr.insert("123a");
Setstr.insert("abd");
for_each(Setstr.begin(), Setstr.end(), Show);
system("pause");
return 0;
}
此程序1:应用到3点:
1)for_each 第三个参数是函数指针,只需要一个函数名/函数指针。
2)函数指针的 参数默认与前面的两个参数的解引用/ 实际值有关;
3) set可以添加同一个键,但是会被相同的覆盖掉;同理:map;
///程序2
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
// function object that adds the value with which it is initialized
class AddValue {
private:
int theValue; // the value to add
public:
// constructor initializes the value to add
AddValue(int v) : theValue(v) {
}
// the ``function call'' for the element adds the value
void operator() (int& elem) const {
elem += theValue;
}
};
void f(int i)
{
cout << i << endl;
}
int main()
{
list<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
cout << "initialized: "<< endl;
for_each(coll.begin(), coll.end(), f); // range 遍历输出
// add value 10 to each element
for_each (coll.begin(), coll.end(), // range
AddValue(10)); // operation
//第一次遍历时候为 AddValue(10)(*coll.begin())
cout << "after adding 10:: "<< endl;
for_each(coll.begin(), coll.end(), // range
f);
// add value of first element to each element
for_each (coll.begin(), coll.end(), // range
AddValue(*coll.begin())); // operation
//PRINT_ELEMENTS(coll,"after adding first element: ");
cout <<"after adding first element:"<< endl;
for_each(coll.begin(), coll.end(), // range
f);
system("pause");
}
//程序3
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <map>
using namespace std;
map <int ,int> mapInt;
void f(pair<int, int> p)
{
cout << p.first<< " "<< p.second << endl;
}
int main(int argc, char * argv[])
{
int i = 0;
mapInt.insert(make_pair(i, i));
i = 1;
mapInt.insert(make_pair(i, i));
i = 0;
mapInt.insert(make_pair(i, i));
for_each(mapInt.begin(), mapInt.end(), f);
system("pause");
return 0;
}
//程序3:可以看到如何使用 pair类型
//程序1
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> Setstr;
void Show(string sr)
{
cout <<" "<< sr.c_str() << endl;
}
int main(int argc, char * argv[])
{
Setstr.insert("abd");
Setstr.insert("123a");
Setstr.insert("abd");
for_each(Setstr.begin(), Setstr.end(), Show);
system("pause");
return 0;
}
此程序1:应用到3点:
1)for_each 第三个参数是函数指针,只需要一个函数名/函数指针。
2)函数指针的 参数默认与前面的两个参数的解引用/ 实际值有关;
3) set可以添加同一个键,但是会被相同的覆盖掉;同理:map;
///程序2
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
// function object that adds the value with which it is initialized
class AddValue {
private:
int theValue; // the value to add
public:
// constructor initializes the value to add
AddValue(int v) : theValue(v) {
}
// the ``function call'' for the element adds the value
void operator() (int& elem) const {
elem += theValue;
}
};
void f(int i)
{
cout << i << endl;
}
int main()
{
list<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
cout << "initialized: "<< endl;
for_each(coll.begin(), coll.end(), f); // range 遍历输出
// add value 10 to each element
for_each (coll.begin(), coll.end(), // range
AddValue(10)); // operation
//第一次遍历时候为 AddValue(10)(*coll.begin())
cout << "after adding 10:: "<< endl;
for_each(coll.begin(), coll.end(), // range
f);
// add value of first element to each element
for_each (coll.begin(), coll.end(), // range
AddValue(*coll.begin())); // operation
//PRINT_ELEMENTS(coll,"after adding first element: ");
cout <<"after adding first element:"<< endl;
for_each(coll.begin(), coll.end(), // range
f);
system("pause");
}
//程序3
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <map>
using namespace std;
map <int ,int> mapInt;
void f(pair<int, int> p)
{
cout << p.first<< " "<< p.second << endl;
}
int main(int argc, char * argv[])
{
int i = 0;
mapInt.insert(make_pair(i, i));
i = 1;
mapInt.insert(make_pair(i, i));
i = 0;
mapInt.insert(make_pair(i, i));
for_each(mapInt.begin(), mapInt.end(), f);
system("pause");
return 0;
}
//程序3:可以看到如何使用 pair类型
相关文章推荐
- for_each各种情况下的函数对象使用方法
- for_each()使用函数对象
- STL for_each 和函数对象(function object) 使用详解
- 函数对象状态(Function Object State)获取 -- 引用传递和for_each()
- 为什么函数对象在for_each中会析构三次
- ArcGIS API for JavaScript 4.2学习笔记[20] 使用缓冲区结合Query对象进行地震点查询【重温异步操作思想】
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- 使用for-each删除list中的对象抛出 java.util.ConcurrentModificationException异常
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- 函数对象 与 count_if()结合使用 例子
- STL:从for_each到函数对象再到bind函数
- c++11 for_each结合lambda表达式的使用 高效优雅的代码 有代码有真相
- javascript创建对象之函数构造模式和原型模式结合使用(四)
- for_each()在函数对象中的独特作用
- 【jquery】jquery使用$.each函数遍历对象
- datawindow自动换行打印,需结合该函数一起使用
- PHP 使用list函数each函数遍历数组(实现foreach)解析
- 不使用for、while等循环实现strlen()函数
- 从零开始学C++之对象的使用(一):static 成员变量、static 成员函数、类/对象的大小
- 类,对象,成员函数使用实质