【STL】for_each使用示例
2016-08-03 15:14
549 查看
[cpp] view
plain copy
print?
void myfun1(int& i)
{
std::cout << i << " ";
}
void myfun2(int i, const char* prefix)
{
std::cout << prefix << i << std::endl;
}
struct mystruct1 {
void operator() (int& i)
{
std::cout << i << " ";
}
} myobject1;
struct mystruct2 {
const char* prefix_;
mystruct2(const char* prefix): prefix_(prefix) {}
void operator() (int& i) {
std::cout << prefix_ << i << std::endl;
}
};
class myclass1
{
public:
void operator() (int& i)
{
std::cout << i << " ";
}
};
class myclass2
{
public:
const char* prefix_;
myclass2(const char* prefix): prefix_(prefix){};
void operator() (int& i)
{
std::cout << i << " ";
}
};
void test_for_each()
{
int ar[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> v(ar, ar + 9);
// 一个参数的普通函数
std::cout << "myfunction1 output below: " << std::endl;
for_each(v.begin(), v.end(), myfun1);
std::cout << std::endl;
// 二个参数的普通函数
std::cout << "myfunction2 output below: " << std::endl;
for_each(v.begin(), v.end(), std::bind2nd(std::ptr_fun(myfun2), "i = "));
// 不带参的结构体
std::cout << "mystruct1 output below: " << std::endl;
for_each(v.begin(), v.end(), mystruct1());
// for_each(v.begin(), v.end(), myobject1); // 这个方法也行.
std::cout << std::endl;
// 带参的结构体
std::cout << "mystruct2 output below: " << std::endl;
for_each(v.begin(), v.end(), mystruct2("i = "));
// 不带参的类
std::cout << "myclass1 output below: " << std::endl;
for_each(v.begin(), v.end(), myclass1());
std::cout << std::endl;
// 带参的类
std::cout << "myclass2 output below: " << std::endl;
for_each(v.begin(), v.end(), myclass2("i = "));
std::cout << std::endl;
}
plain copy
print?
void myfun1(int& i)
{
std::cout << i << " ";
}
void myfun2(int i, const char* prefix)
{
std::cout << prefix << i << std::endl;
}
struct mystruct1 {
void operator() (int& i)
{
std::cout << i << " ";
}
} myobject1;
struct mystruct2 {
const char* prefix_;
mystruct2(const char* prefix): prefix_(prefix) {}
void operator() (int& i) {
std::cout << prefix_ << i << std::endl;
}
};
class myclass1
{
public:
void operator() (int& i)
{
std::cout << i << " ";
}
};
class myclass2
{
public:
const char* prefix_;
myclass2(const char* prefix): prefix_(prefix){};
void operator() (int& i)
{
std::cout << i << " ";
}
};
void test_for_each()
{
int ar[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> v(ar, ar + 9);
// 一个参数的普通函数
std::cout << "myfunction1 output below: " << std::endl;
for_each(v.begin(), v.end(), myfun1);
std::cout << std::endl;
// 二个参数的普通函数
std::cout << "myfunction2 output below: " << std::endl;
for_each(v.begin(), v.end(), std::bind2nd(std::ptr_fun(myfun2), "i = "));
// 不带参的结构体
std::cout << "mystruct1 output below: " << std::endl;
for_each(v.begin(), v.end(), mystruct1());
// for_each(v.begin(), v.end(), myobject1); // 这个方法也行.
std::cout << std::endl;
// 带参的结构体
std::cout << "mystruct2 output below: " << std::endl;
for_each(v.begin(), v.end(), mystruct2("i = "));
// 不带参的类
std::cout << "myclass1 output below: " << std::endl;
for_each(v.begin(), v.end(), myclass1());
std::cout << std::endl;
// 带参的类
std::cout << "myclass2 output below: " << std::endl;
for_each(v.begin(), v.end(), myclass2("i = "));
std::cout << std::endl;
}
相关文章推荐
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- (原創) 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- STL的适配器、仿函数学习之一:accumulate和for_each的使用心得
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- jQuery使用each方法与for语句遍历数组示例
- 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- stl for_each使用
- 【STL】for_each示例
- STL之for_each, find_if,adjacent_find,count_if的使用
- STL for_each 和函数对象(function object) 使用详解
- 【C++ STL应用与实现】26: 如何使用std::for_each以及基于范围的for循环 (since C++11)
- STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- STL 实践(for_each() getline sort random_shuffle的使用)