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. BZOJ 3129 SDOI2013 方程

    如果没有限制,答案直接用隔板法C(m-1,n-1) 对于>=x的限制,我们直接在对应位置先放上x-1即可,即m=m-(x-1) 对于<=x的限制,由于限制很小我们可以利用容斥原理将它转化为 ...

  2. 用javascript编写的简单计算器

    老师布置的任务,弄了一天,总算把代码搞出来了,作为菜鸟给自己一点小掌声,传说中JS是很简单的,但是要写出一个程序真的很需要锻炼,我的锻炼实在是太少了,天天DOTA中.呵呵(做了些小注释)大家应该能看懂 ...

  3. 2014--9=17 软工二班 MyEclipse blue==5

    package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...

  4. ArcEngine查询、添加、删除要数的方法

    原文地址:http://www.cnblogs.com/caodajieup/archive/2011/11/02/2232658.html 1.查找数据 1).利用FeaturCursor进行空间查 ...

  5. Spring的父子容器问题

    在ssm框架搭建的时候 配置了一个Spring容器,又配置了一个前端控制器 <!-- 初始化spring容器 --> <context-param> <param-nam ...

  6. C++:函数模板与模板函数

    6.1 模板的概念 C++允许用同一个函数定义函数,这些函数的参数个数和参数类型不同.例如求最大值的max函数, int max(int x,int y) {       return (x>y ...

  7. 如何写科技文章的讨论discussion部分

    众所周知,讨论部分是在结合自己的研究结果基础上,对整个文章的结论的提炼和升华.这一部分是整个论文的精,往往点睛作用. 同时,很多杂志要求结果和讨论分开,这也就更突出了写好讨论的重要性. 那么,我们应该 ...

  8. oracle .bash_profile

    [oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...

  9. mysql备份恢复数据库据/表

    备份单个数据库,只备份表,如要恢复,必须先创建一个数据库[root@s]# mysqldump -u root -p dbname1 > dbname1.sql[root@s]# mysql - ...

  10. Save output to a text file from Mac terminal

      Simply with output redirection: system_profiler > file.txt Basically, this will take the output ...