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

#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. Android DES AES MD5加密

    AES加密: <span style="font-size:18px;">package com.example.encrypdate.util; import jav ...

  2. linux内核的冒险md来源释义# 14raid5非条块读

    linux内核的冒险md来源释义# 14raid5非条块读 转载请注明出处:http://blog.csdn.net/liumangxiong 假设是非条块内读.那么就至少涉及到两个条块的读,这就须要 ...

  3. 利用PhantomJS进行网页截屏

    利用PhantomJS进行网页截屏 关于PhantomJS PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种W ...

  4. leetcode第24题--Reverse Nodes in k-Group

    problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...

  5. .NET 相依性注入

    发布<.NET 依賴注入>电子书 beta 版 书籍进度 本书目前已经开始发行 beta 版,完成进度约 70%.(我希望这本书不要超过 200 页,目前看起来应该没问题.) 简介 本书内 ...

  6. Linux Shell 示例精解(第七章 gawk编程)转载

    第七章 gawk功能:gawk编程 7.1.1 数字和字符串常量     初始化和类型强制  在awk中,变量不需要定义就可以直接使用,使用一个变量就是对变量的定义.变量的类型可以试数字.字符串,或者 ...

  7. MVC5搜索/查询 流程功能的实现

    接着上次的篇幅,我们这篇手动来写一个查询的流程代码!  搜索/查询 流程功能的实现 那现在要做搜索(查询)功能我们第一步应该做什么呢!第一次是不是我们应该去Controller(控制器)里去搞一个搜索 ...

  8. [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website

    CSS3 continues to both excite and frustrate web designers and developers. We are excited about the p ...

  9. 如何在ubuntu 12.04下搭建Python Django环境

    1. 检查python是否安装:直接在shell里输入python,如果已经安装了python,即可进入python bash,并看到版本号(如Python 2.7.3) ——在ubuntu中pyth ...

  10. 大数据工具篇之Hive与HBase整合完整教程

    大数据工具篇之Hive与HBase整合完整教程 一.引言 最近的一次培训,用户特意提到Hadoop环境下HDFS中存储的文件如何才能导入到HBase,关于这部分基于HBase Java API的写入方 ...