c++ STL常用算法使用方法
#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常用算法使用方法的更多相关文章
- [C++ STL] 常用算法总结
1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...
- STL常用结构与方法简明总结
C++常用的数据结构 序列式容器 vector(向量.有序数列),list(双向链表),deque(双端队列) 适配器容器 stack(栈),queue(队列) 关联式容器 map(映射.键值对二叉树 ...
- STL常用容器使用方法
在程序头部使用#include<stack>来引入STL的stack容器,然后使用stack<int> s语句来声明一个管理整型数据的容器s.stack常用成员函数:push( ...
- 28.STL常用算法
#include <algorithm> 算法 常用版本 描述 返回Type std::find() find(_InIt _Fisrt,_InIt _Last, _Ty& _Va ...
- C++ STL——常用算法
目录 一 常用查找算法 二 常用遍历算法 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 常用查找算法 /* find算法 查找元素 @param ...
- 介绍C++ STL常用模板使用方法的相关资料
1.vector的几种初始化及赋值方式
- GIS常用算法
目录 1.常用算法 1.1.计算两经纬度点之间的距离 1.2.根据已知线段以及到起点距离,求目标点坐标 1.3.已知点.线段,求垂足 1.4.线段上距离目标点最近的点 1.5.点缓冲 1.6.点和面关 ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
随机推荐
- jade的特点
jade特点 1超强的可读性 2灵活易用的缩进 3块扩展 4代码默认进过编码处理,以增强安全性 5编译及运行时的上下文错误报告 6命令行编译支持 7html5模式(使用 !!!5文档类型) 8可选的内 ...
- UVa 532 - Dungeon Master
题目大意:给一个三维迷宫,给出入口和出口,找出最短路径. 无权图上的单源最短路问题,使用BFS解决. #include <cstdio> #include <queue> #i ...
- 使用nodejs爬取和讯网高管增减持数据
为了抓取和讯网高管增减持的数据,首先得分析一下数据的来源: 网址: http://stockdata.stock.hexun.com/ggzjc/history.shtml 使用chrome开发者工具 ...
- 最通用的ibatis.Net使用sql server存储过程返回分页数据的详细例子
ibatis.Net是一个比较简单和灵活的ORM框架,今天我分享一个我的项目中使用sql server通用存储过程来分页的一个例子,用ibatis.Net框架统一返回分页数据为IList<Has ...
- Quartz2D 之 绘制文本
1. 基础概念 1.1. 字体(Font) 同一大小.同一样式的字形的集合. 1.2. 字符(Character) 字符表示信息本身,一般指某种编码,如Unicode编码. 1.3. 字形(Glyph ...
- 判断移动端设备: navigator.userAgent.toLowerCase()
判断你的浏览设备: navigator.userAgent.toLowerCase(); (返回当前用户所使用的是什么浏览器,将获得的信息变成小写) function browserRedirect( ...
- Delphi判断一个字符是否为汉字的最佳方法
//判断字符是否是汉字 function IsHZ(ch: WideChar): boolean; var i:integer; begin i:=ord(ch); if( i<19968) o ...
- js观察者模式与Model
目的 观察者模式是常见的设计模式,可以被应用到MV*框架的Model上,来实现对数据变化的监听. 基本概念 观察者模式是一种常见的设计模式.被观察者可以被订阅(subscribe),并在状态发生改变时 ...
- Docker,容器,虚拟机和红烧肉
Docker火了,有多火你自己看看下面的统计数据就知道了 在发布4个月的时间里,下载量就超过50000次,github上收到超过4000个star,涌现了超过100个贡献者,并且有超过150个项目和超 ...
- Postman编程
Postman常用Api Postman像jmeter一样提供前置处理脚本和后置处理脚本.脚本主要使用JavaScript语法,并内置提供了一些js代码库,提供了一些内置对象和方法. 参考:https ...