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. LR_问题_脚本运行时提示没有参数化

    问题描述 将loadrunner中的参数删除,并且删除脚本目录下对应的bak,执行脚本,出现下面的错误: “错误: 表“XX\phonenumbser.dat”不存在.         [MsgId: ...

  2. go的优缺点

    1.1 不允许左花括号另起一行1.2 编译器莫名其妙地给行尾加上分号1.3 极度强调编译速度,不惜放弃本应提供的功能1.4 错误处理机制太原始1.5 垃圾回收器(GC)不完善.有重大缺陷1.6 禁止未 ...

  3. PowerDesinger逆向数据库物理模型及关系图

    原文:PowerDesinger逆向数据库物理模型及关系图 利用PowerDesinger生成的数据库物理模型及关系图 收集五年的开发资料下载地址:  http://pan.baidu.com/sha ...

  4. PostgreSql中如何kill掉正在执行的sql语句

    虽然可以使用 kill -9 来强制删除用户进程,但是不建议这么去做. 因为:对于执行update的语句来说,kill掉进程,可能会导致Postgres进入到recovery mode 而在recov ...

  5. redhat6和ubuntu13.10在WMware player 下与Windows共享文件

    Redhat下: 点击VMware的 setting -> vmware tools install mount /dev/cdrom /mnt/cdromcd /mnt/cdrom里面有一个v ...

  6. JNIEnv解析

    1.关于JNIEnv和JavaVM JNIEnv:线程相关的变量 JavaVM:是虚拟机在JNI层的代表, JNIEnv是一个与线程相关的变量,不同线程的JNIEnv彼此独立.JavaVM是虚拟机在J ...

  7. oracle db shutdown immediate–multi Instance

    [oracle@redhat4 ~]$ sqlplus / as sysdba@orcl SQL*Plus: Release 11.2.0.1.0 Production on Tue Oct 6 21 ...

  8. bzoj3747

    经典题,记录每个位置对应数下次出现的位置next[i] 每个位置维护当前左端点下到这个位置的和 随着左端点的右移一位到i+1,对[i+1,next[i]-1] 的影响是-a[i], [next[i], ...

  9. PHPnow 升级后 PHP不支持GD、MySQL

    来自http://tunps.com/php-unsupport-gd-and-mysql-after-upgrade-phpnow 最近磁盘格式化误操作后,最近两天都在忙于数据恢复,现在才恢复正常. ...

  10. NYOJ 536 开心的mdd【矩阵链乘】

    题意:给出n个矩阵组成的序列,问最少的运算量 看的紫书: dp[i][j]表示从第i个矩阵到第j个矩阵最少的乘法次数 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j] ...