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查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
随机推荐
- IOS开发-UI学习-sqlite数据库的操作
IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...
- 3)Javascript设计模式:Observer模式
Observer模式 var Observer = (function() { var instance = null; function Observe() { this.events = {} } ...
- SpannableString富文本
忍不住想吐槽这个类,这个类是要给文本设置不同的颜色.字体样式 例子:一句话中只有某几个文字想要设置成不同的颜色 起初写了一个函数setColorStyle(), public SpannableStr ...
- BZOJ 2179FFT快速傅立叶
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2179 题目大意:给出两个n位10进制整数x和y,你需要计算x*y. 题解:FFT,不会的可以 ...
- TCP carries HTTP data in order, and without corruption
wTCP/IP承载几乎所有的HTTP通信. HTTP The Definitive Guide
- PHP文件上传主要代码讲解
导读:在php开发过程中,文件上传也经常用到,这里简单介绍下. 在php开发过程中,文件上传也经常用到,这里简单介绍下. 代码如下: <?php if($_FILES['myfile'][ ...
- MySQL安装 MySQL5.7.10免安装版配置,mysql5.7.10免安装版
MySQL5.7.10免安装版配置,mysql5.7.10免安装版 最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: ...
- Eclipse 注释
Ctrl + / Ctri + Shift + / Ctrl + Shift + C
- B-JUI 实践 之 带搜索与编辑的Datagrid
使用B-JUI 1.31版本 DataGrid 页直接上代码: <div class="bjui-pageHeader" style="background-col ...
- C#综合揭秘——细说进程、应用程序域与上下文之间的关系
引言 本文主要是介绍进程(Process).应用程序域(AppDomain)..NET上下文(Context)的概念与操作.虽然在一般的开发当中这三者并不常用,但熟悉三者的关系,深入了解其作用,对提高 ...