STL_算法_for_each
来源:互联网 发布:和讯网数据 编辑:程序博客网 时间:2024/05/29 15:08
C++ Primer 学习中。。。
简单记录下我的学习过程 (代码为主)
所有容器适用
for_each(b,e,p)
1、使用for_each()算法遍历数据
2、使用for_each()和函数对象修改数据
3、使用for_each()的返回值
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;/*****************************************//所有容器适用for_each(b,e,p)使用for_each()算法遍历数据使用for_each()和函数对象修改数据使用for_each()的返回值*****************************************//*************************************************************************************std::for_each 所有排序容器适用 algorithm--------------------------------------------------------------------------------------template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function f);//eg:template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f) { for ( ; first!=last; ++first ) f(*first); return f; }*************************************************************************************/void myfunction (int i){ cout << " " << i;}struct myclass{ void operator() (int i) { cout << " " << i; }} myobject;class MeanVlaue{public: MeanVlaue():num(0),sum(0){} void operator() (int elem) { num++; sum+=elem; } double value() { return static_cast<double>(sum)/static_cast<double>(num); } operator double()//重载的好处,简洁! { return static_cast<double>(sum)/static_cast<double>(num); }private: long num; long sum;};template<class T>class AddValue{public: AddValue(const T& v):theValue(v){} void operator() (T& elem)const { elem+=theValue; }private: T theValue;};int main(){ vector<int> myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); // or: cout << "\nmyvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); cout << endl; MeanVlaue mv=for_each(myvector.begin(),myvector.end(),MeanVlaue()); cout<<"平均值:"<<mv.value()<<endl; for_each(myvector.begin(), myvector.end(), AddValue<int>(10));//参数可以自己改,eg:*(myvector.begin()) for_each (myvector.begin(), myvector.end(), myobject); cout << endl; double mv2=for_each(myvector.begin(),myvector.end(),MeanVlaue()); cout<<"平均值:"<<mv2<<endl; return 0;}
1 0
- STL_算法_for_each
- STL_算法_transform
- STL_算法(30)_Heap算法
- STL_算法(22)_ STL_算法_替换算法
- STL_算法(21)_ STL_算法_填充新值
- STL_算法_交换(swap_ranges)
- STL_算法_查找算法(search_n)
- STL_算法_查找算法(find_first_of、find_last_of)
- STL_算法_查找算法(adjacent_find)
- STL_算法_查找算法(binary_search、includes)
- STL_算法_元素计数(count、count_if)
- STL_算法_复制元素(copy、copy_backward)
- STL_算法 for_each 和 transform 比较
- STL_算法_删除(unique、unique_copy)
- STL_算法_逆转(reverse,reverse_copy)
- STL_算法_旋转(rotate、rotate_copy)
- STL_算法_排列(prev_permutation、next_permutation)
- STL_算法_局部排序(partial_sort、partial_sort_copy)
- Android 5.0学习之AnimatedVectorDrawable
- 删除数组制定元素或下标
- Hibernate 创建 sessionFatory
- 第1章第2节练习题19 判断循环双链表对称
- android NDK添加iconv支持,支持GBK UTF-8编码转换
- STL_算法_for_each
- nginx thinkPHP 部署项目出现的相关问题
- 编程修养(二)
- Http状态码
- 近三个月的总结与新阶段的展望
- JAVA内存处理--堆和栈
- UITableView - 全面解析
- 编程修养(三)
- 笨办法学Python学习笔记 练习25