#include <string>
#include <vector>
#include <functional>
#include <iostream>
using namespace std; void print(vector<int>& list, const string &des)
{
cout<<"after "<<des.c_str()<<", the list is: "<<endl;
for(vector<int>::iterator iter = list.begin(); iter != list.end(); ++iter)
{
cout<<(*iter)<<" ";
}
cout<<endl;
} void printstr(vector<string>& list, const string &des)
{
cout<<"after "<<des.c_str()<<", the list is: "<<endl;
for(vector<string>::iterator iter = list.begin(); iter != list.end(); ++iter)
{
cout<<(*iter)<<" ";
}
cout<<endl;
} template<class T>
struct comp:public std::binary_function<T,T,bool>
{
bool operator()(const T &t1, const T &t2) const
{
return t1 > t2;
}
}; struct conv : public std::unary_function<string, string>
{
string& operator()(string &str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
}; int main()
{
vector<int> list;
list.push_back(2324);
list.push_back(45);
list.push_back(576);
list.push_back(768);
list.push_back(21);
list.push_back(4);
list.push_back(1);
list.push_back(54758);
list.push_back(123);
list.push_back(464);
list.push_back(88);
print(list, "push back"); std::sort(list.begin(), list.end());
print(list, "normal sort"); vector<int>::iterator iter = lower_bound(list.begin(), list.end(), 5);
cout<<"lower find element is 5, value "<<(*iter)<<endl; iter = upper_bound(list.begin(), list.end(), 5);
cout<<"upper find element is 5, value "<<(*iter)<<endl; bool ret = std::binary_search(list.begin(), list.end(), 464);
if(ret)
{
cout<<"find 464, value ok."<<endl;
} std::sort(list.begin(), list.end(), comp<int>());
print(list, "big sort"); iter = lower_bound(list.begin(), list.end(), 5);
cout<<"lower find element is 5, value "<<(*iter)<<endl; iter = upper_bound(list.begin(), list.end(), 5);
cout<<"upper find element is 5, value "<<(*iter)<<endl; ret = std::binary_search(list.begin(), list.end(), 88, comp<int>());
if(ret)
{
cout<<"find 88, value ok."<<endl;
} list.insert(std::upper_bound(list.begin(), list.end(), 55, comp<int>()), 55);
print(list, "upper bound insert"); list.insert(std::lower_bound(list.begin(), list.end(), 77, comp<int>()), 77);
print(list, "lower bound insert"); iter = std::search(list.begin(), list.end(), list.begin() + 4, list.begin() + 6);
if(iter != list.end())
{
cout<<"search seq pos is "<<(*iter)<<endl;
} vector<string> strList;
strList.push_back("AAAA");
strList.push_back("BBBB");
strList.push_back("CCCC");
printstr(strList,"str list"); std::transform(strList.begin(), strList.end(), strList.begin(), conv());
printstr(strList, "transform"); // sort之后才能执行unique return 0;
}

  上面的使用主要起到抛砖引玉的作用,可根据实际情况借鉴使用方法使用其它算法。

c++ STL常用算法使用方法的更多相关文章

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

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

  2. STL常用结构与方法简明总结

    C++常用的数据结构 序列式容器 vector(向量.有序数列),list(双向链表),deque(双端队列) 适配器容器 stack(栈),queue(队列) 关联式容器 map(映射.键值对二叉树 ...

  3. STL常用容器使用方法

    在程序头部使用#include<stack>来引入STL的stack容器,然后使用stack<int> s语句来声明一个管理整型数据的容器s.stack常用成员函数:push( ...

  4. 28.STL常用算法

    #include <algorithm> 算法 常用版本 描述 返回Type std::find() find(_InIt _Fisrt,_InIt _Last, _Ty& _Va ...

  5. C++ STL——常用算法

    目录 一 常用查找算法 二 常用遍历算法 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 常用查找算法 /* find算法 查找元素 @param ...

  6. 介绍C++ STL常用模板使用方法的相关资料

    1.vector的几种初始化及赋值方式

  7. GIS常用算法

    目录 1.常用算法 1.1.计算两经纬度点之间的距离 1.2.根据已知线段以及到起点距离,求目标点坐标 1.3.已知点.线段,求垂足 1.4.线段上距离目标点最近的点 1.5.点缓冲 1.6.点和面关 ...

  8. C++ STL 常用遍历算法

    C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...

  9. 常用的STL查找算法

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

随机推荐

  1. Uber广州车主官网本周将暂关闭

    Uber广州车主官网本周将暂关闭 http://news.southcn.com/g/2015-05/04/content_123509931.htm

  2. wordpress-标签

    来源1:http://www.graphicrating.com/2009/01/18/my-wordpress-cheat-sheet/ 来源2:http://www.cnblogs.com/asq ...

  3. Android项目文件结构

    一.Project项目结构 app/build/ app模块build编译输出的目录 app/build.gradle app模块的gradle编译文件 app/app.iml app模块的配置文件 ...

  4. ubuntu 安装chrome浏览器

    对于一个一直用chrome的人来说,真的很不习惯用firefox,虽然firebug很好用.. 好吧,心情不佳,直接上命令: 下载命令(有墙,下不下来,多试几次): wget https://dl.g ...

  5. iOS 之 UITextView

    _lableAssess = [[UITextView alloc] init]; [_lableAssess setFrame:CGRectMake(left2, top2, width2, siz ...

  6. centos 6.5下安装、配置并启动SSH远程访问

    centos 6.5下安装.配置并启动SSH远程访问 1.登录centos 6.5系统,使用root用户登录,如果为非root用户则执行su或su - 或su root或su - root切换为roo ...

  7. 无法打开登录 'ASPState' 中请求的数据库。登录失败。

    问题: 无法打开登录 'ASPState' 中请求的数据库.登录失败.用户 'WH\Administrator' 登录失败. 解决方法: (启动SQL Server Agent服务) 从本系统中找到: ...

  8. mysql调优 参数说明

    原文地址:http://wenku.baidu.com/view/49087bbafd0a79563c1e72c8.html 关键字: mysql *1, 查看 MySQL 服务器配置信息 *1. m ...

  9. win7配置自己的IIS服务器亲自做的图文很详细

    跟人网站爱好初学者必看的win7系统配置自己的IIS,可以在你自己的电脑上配置网站服务器发不到网上,下面就跟着我的步骤一起做吧100%成功. 步骤/方法     点击开始-------控制面板这个就是 ...

  10. easyUI progressbar组件

    easyUI progressbar组件: <!DOCTYPE html> <html lang="en"> <head> <meta c ...