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查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
随机推荐
- PHPEXCEL实例-导出EXCEL
PHPExcel 是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择. <?php /* * 导出EXCEL * ...
- Object.create() 实现
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototyp ...
- 使用布局(Layout资源)
实际上从我们学习第一个Android应用开始,已经开始接触Android的Layout资源了,因此此处不会详细介绍Android Layout资源的知识,会对Layout资源进行简单的归纳. Layo ...
- HDU1548:A strange lift(Dijkstra或BFS)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:电梯每层有一个数,例如第n层有个数k, 那么这一层只能上k层或下k层,但是不能低于一层或高 ...
- IIS8中添加WCF支持几种方法小结[图文]
方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...
- ASP.NET - 自定义控件处理页面事件(控件与页面数据交互)的方法
//用委托的方法实现 //控件代码 public delegate void DelegateFunction( string sPageTitle ); private DelegateFuncti ...
- localStorage的黑科技-js和css缓存机制
一.发现黑科技的起因 今天在微信公众号看到一篇技术博文,想用印象笔记收藏,所以发送了文章链接到pc上.然后习惯性地打开控制台,看看源码,想了解下最近微信用了什么新技术. 呵呵,以下勾起了我侦探的欲 ...
- 对lua中_ENV表的理解(lua5.2版本以后)
当我拿到_ENV表的时候,会去想这个_ENV表是干什么用的? 首先看如下代码: print(_ENV) --0x1d005f0 print(_G) --0x1d005f0 ViewCode 看了上面的 ...
- 转载:MAT Memory Analyzer Tool使用示例
地址:http://blog.csdn.net/yanghongchang_/article/details/7711911 以下是一个会导致java.lang.OutOfMemoryError: J ...
- 一个web应用的诞生--美化一下
经过上一章的内容,其实就页面层来说已结可以很轻松的实现功能了,但是很明显美观上还有很大的欠缺,现在有一些很好的前端css框架,如AmazeUI,腾讯的WeUI等等,这里推荐一个和flask集成很好的b ...