STL泛型算法

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <list>
using std::cout;
using std::endl;
using std::vector;
using std::list; bool IsOushu(const int& nNum);
bool IsBigger(const int& nFirst, const int& nSecond); int main()
{
vector<int> iVec;
for(int i = ; i < ; ++ i)
iVec.push_back(i); cout << endl;
typedef vector<int> IVEC; //std::find
IVEC::const_iterator iter = std::find(iVec.begin(), iVec.end(), );
if(iVec.end() != iter)
cout << endl << "The value is " << *iter << endl;
else
cout << endl << "Can not find the value " << << endl; //std::accumulate
int nSum = std::accumulate(iVec.begin(), iVec.end(), );
cout << endl << "The sum is " << nSum << endl; cout << endl; //fill
vector<int> iVec2();
std::fill(iVec2.begin(), iVec2.end(), );
for(IVEC::const_iterator iter = iVec2.begin(); iter != iVec2.end(); ++ iter)
cout << *iter << ", "; cout << endl; //fill_n
vector<int> iVec3();
std::fill_n(back_inserter(iVec3), , );
cout << endl << "size of iVec3 is " << iVec3.size() << endl;
for(IVEC::const_iterator iter = iVec3.begin(); iter != iVec3.end(); ++ iter)
cout << *iter << ", ";
cout << endl; cout << endl; //copy
vector<int> iVec4;
list<int> lst1;
for(int i = ; i < ; ++ i)
lst1.push_back(i); std::copy(lst1.begin(), lst1.end(), back_inserter(iVec4));
for(IVEC::const_iterator iter = iVec4.begin(); iter != iVec4.end(); ++ iter)
cout << *iter << ", ";
cout << endl << endl; //copy
vector<int> iVec5();
std::copy(lst1.begin(), lst1.end(), iVec5.begin());
for(IVEC::const_iterator iter = iVec5.begin(); iter != iVec5.end(); ++ iter)
cout << *iter << ", ";
cout << endl << endl; //replace
list<int> lst2;
for(int i = ; i < ; ++ i)
lst2.push_back(i * );
cout << endl; //打印replace之前到值
cout << endl << "打印lst2 replace之前到值 " << endl;
for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter)
cout << *iter << ", ";
cout << endl;
cout << "打印replace之后到值 " << endl;
std::replace(lst2.begin(), lst2.end(), , );
for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter)
cout << *iter << ", ";
cout << endl; cout << endl;
//replace_copy
list<int> lst3(lst2.size());
std::replace_copy(lst2.begin(), lst2.end(), lst3.begin(), , );
cout << endl << "打印lst2 replace_copy 之后 lst3 到值 " << endl;
for(list<int>::const_iterator iter = lst3.begin(); iter != lst3.end(); ++ iter)
cout << *iter << ", ";
cout << endl; //stable_sort
vector<int> iVec6;
for(int i = ; i < ; ++ i)
iVec6.push_back(i);
cout << endl;
cout << endl << "打印stable_sort之前到iVec6到值 " << endl;
for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter)
cout << *iter << ", ";
cout << endl << "打印stable_sort之后到iVec6到值 " << endl; std::stable_sort(iVec6.begin(), iVec6.end(), IsBigger); for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter)
cout << *iter << ", ";
cout << endl << endl; //count_if
cout << endl << "计算iVec6中偶数到个数 " << endl;
int nNums = std::count_if(iVec6.begin(), iVec6.end(), IsOushu);
cout << endl << "iVec6中偶数个数为 " << nNums <<" 个" << endl; cout << endl << endl; cout << "\nThis is main function \n";
return ; } //stable_sort 降序排列
bool IsBigger(const int& nFirst, const int& nSecond)
{
return nFirst > nSecond;
} //是偶数
bool IsOushu(const int& nNum)
{
return ( == nNum % );
}

执行结果

【STL】帮你复习STL泛型算法 一的更多相关文章

  1. STL的一些泛型算法

    源地址:http://blog.csdn.net/byijie/article/details/8142859 从福州大学资料里摘下来的我现在能理解的泛型算法 algorithm min(a,b) 返 ...

  2. STL区间成员函数及区间算法总结

    STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...

  3. stl之容器、迭代器、算法几者之间的关系

    转自:https://blog.csdn.net/bobodem/article/details/49386131 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的优 ...

  4. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  5. 泛型编程、STL的概念、STL模板思想及其六大组件的关系,以及泛型编程(GP)、STL、面向对象编程(OOP)、C++之间的关系

    2013-08-11 10:46:39 介绍STL模板的书,有两本比较经典: 一本是<Generic Programming and the STL>,中文翻译为<泛型编程与STL& ...

  6. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  7. C++ 泛型算法

    <C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...

  8. C++的那些事:容器和泛型算法

    一.顺序容器 1,标准库定义了3种类型的顺序容器:vector.list和deque.它们的差别主要在于访问元素的方式,以及添加或删除元素相关操作运算代价.标准库还提供了三种容器适配器:stack.q ...

  9. C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法

    大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ●  find算法,算法接受一对迭代 ...

随机推荐

  1. NOIP 2015提高组复赛

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...

  2. 保障视频4G传输的流畅性,海康威视摄像头相关设置

    我们目前的rtsp视频解决方案如下: 摄像头<---------->NVR(通过4G上传)<---------->easydarwin<---------->自己的 ...

  3. java:静态成员变量和静态函数

    静态成员变量 可以使用类名调用,如 class Dog { static int age; } class Test2{ public static void main(String args[]){ ...

  4. oracle 修改表的sql语句

    oracle 修改表的sql语句     1增加一个列:ALTER TABLE 表名 ADD(列名 数据类型);如:ALTER TABLE emp ADD(license varchar2(256)) ...

  5. NSMutableArray 初始化与添加删除程序

           Person *person1=[[Person alloc]initWithName:@"Kenshin"];        Person *person2=[[P ...

  6. SqlServer几个注意点

    1.修改系统参数时,必须是单用户情况下才能更改成功!在Properties->Options中修改. 2.数据库字段值默认是不区分大小写的,修改方法如下: 2.1.右键数据库,选择Propert ...

  7. 成为一个PHP专家:缺失的环节

    这一篇文章是“Becoming a PHP Professional”系列 4 篇博文中的第 1 篇. 当浏览各类与PHP相关的博客时,比如Quora上的问题,谷歌群组,简讯和杂志,我经常注意到技能的 ...

  8. ORACLE将表中的数据恢复到某一个时间点

    执行如下SQL将test_temp表中的数据恢复到 2013-04-26  21:06:00 注意,这里一定要先删除全部数据,否则可能会导致数据重复 delete from test_temp; in ...

  9. git push

    使用git push直接推送未关联分支的时候,出现如下提示: $ git push Counting objects: 46, done. Delta compression using up to ...

  10. 对List顺序,逆序,随机排列实例代码

    ackage  Test; import  java.util.Collections; import  java.util.LinkedList; import  java.util.List; p ...