通过一个实例来说明这些算法的接口使用:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
using namespace std;
template <class T>
struct display{
void operator()(const T& x)const{
cout<<x<<" ";
}
};
int main()
{
int ia[9]={0,1,2,3,4,5,6,7,8};
vector<int> iv1(ia,ia+5);
vector<int> iv2(ia,ia+9);
cout<<equal(iv1.begin(),iv1.end(),iv2.begin())<<endl;//1,true fill(iv1.begin(),iv1.end(),9);//区间全部填9
for_each(iv1.begin(),iv1.end(),display<int>());
cout<<endl; fill_n(iv1.begin(),3,7);
for_each(iv1.begin(),iv1.end(),display<int>());
cout<<endl; string s1[]={"jamie","jjHou","jason"};
string s2[]={"jamie","jjhou","jerry"};
cout<<lexicographical_compare(s1,s1+2,s2,s2+2)<<endl;
cout<<lexicographical_compare(s1,s1+2,s2,s2+2,greater<string>())<<endl;
//字典序对比,s1小于s2
return 0;
}

在看下面一些算法:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <set>
#include <iterator>
using namespace std;
template <class T> //这是一个函数对象
struct display{
void operator()(const T& x)const{
cout<<x<<" ";
}
};
int main()
{
int ia[]={0,1,2,3,4,5,6,6,6,7,8};
vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));
cout<<count(iv.begin(),iv.end(),6)<<endl;//找出元素值为6的个数
cout<<count_if(iv.begin(),iv.end(),bind2nd(less<int>(),7))<<endl;
cout<<*find_if(iv.begin(),iv.end(),bind2nd(greater<int>(),2))<<endl;
for_each(iv.begin(),iv.end(),display<int>());
cout<<endl;
reverse(iv.begin(),iv.end()); //逆序重排
for_each(iv.begin(),iv.end(),display<int>());//输出每一个元素
cout<<endl;
int ia2[]={0,1,2};
vector<int> iv2(ia2,ia2+3);
vector<int>::iterator ite=find_first_of(iv.begin(),iv.end(),iv2.begin(),iv2.end());
//在第一个区间序列中查找第二序列中任意元素第一次出现的位置,返回该位置的迭代器
while(ite!=iv.end())
{
cout<<*ite<<" ";
ite++;
}
return 0;
}

STL---基本算法---<stl_algobase.h>概述的更多相关文章

  1. STL源代码剖析——基本算法stl_algobase.h

    前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...

  2. STL源代码剖析——STL算法stl_algo.h

    前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...

  3. stl_algobase.h

    stl_algobase.h // Filename: stl_algobase.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  4. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  5. C++复习:STL之算法

    算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...

  6. 初探STL之算法

    算法 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包括头文件<algor ...

  7. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  8. 【STL】帮你复习STL泛型算法 一

    STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...

  9. STL基础--算法(排序)

    STL排序算法 排序算法要求随机访问迭代器 vector, deque, container array, native array 例子 vector<int> vec = {9,1,1 ...

随机推荐

  1. App根据第,HTML5流行?

    App根据第.HTML5流行? 引用新闻 日前,有消息称国家网信办近日将出台APP应用程序发展管理办法.中央网信办主任鲁炜在推进网络空间法治化座谈会上透露.我国将加强互联网立法,依靠严密的法律网来打造 ...

  2. 亮点面试题&amp;&amp;实现Singleton(辛格尔顿)模式-JAVA版本

    称号:设计一个类.我们只能产生这个类的一个实例.(来自<剑指Offer>) 解析:仅仅能生产一个实例的类是实现Singleton(单例)模式的类型.因为设计模式在面向对象程序设计中起着举足 ...

  3. wpa_cli P2P 连接相关的调试命令

    在最近的一次客户端上的调试p2p的wifi display, 他们中的一半Android该调整了,整个前所以没有太多的研究p2p过程连接, 客户现在使用的非Android平台架构. 需要协助客户这么多 ...

  4. Windows Azure

    Windows Azure初体验 目前在IT界,云这个概念的第一意思不再是词典里的解释了.不过它们还是有相同点的——也许确实会酝酿出一块大蛋糕,可也是飘在天上,众神分食之,与我等P民无关.所谓云,不过 ...

  5. 动画操作 (Applying Animations) ngAnimate12

    动画操作 (Applying Animations) ngAnimate step 12 1.切换目录 git checkout step-12 npm start 2.效果图 这里在点击右边的缩略图 ...

  6. C#单元测试工具包:MvcContrib

    C#单元测试工具包:MvcContrib http://t.cn/hE67d https://mvccontrib.codeplex.com/documentation MVC Contrib Doc ...

  7. springmvc的一些记录

    1.springmvc基本配置 2.springmvc+hibernate4配置 3.springmvc+hibernate4+freemarker配置 4.springmvc+hibernate4+ ...

  8. ASP.NET MVC 中的视图生成

    关于 ASP.NET MVC 中的视图生成 在 ASP.NET MVC 中,我们将前端的呈现划分为三个独立的部分来实现,Controller 用来控制用户的操作,View 用来控制呈现的内容,Mode ...

  9. 实现WebService的调用与被调用

    之前一直用WCF来开发服务,可是从未用过WebService.对WebService有种很神奇的期待,都说WebService比较简单,但是从未用过就对我来说就是一种新的知识.起始让我来说WCF与We ...

  10. android中自定义shape

    <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...