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查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
随机推荐
- .NET运行机制
.NET运行机制 .NET框架是一个多语言组件开发和执行环境,它提供了一个跨语言的统一编程环境..NET框架的目的是便于开发人员更容易地建立Web应用程序和Web服务,使得Internet上的各应 ...
- 计时器chronometer补充
项目中要实现关于安卓控件chronometer这部分的功能需求: 1.计时器的功能对用户答题时间进行时间统计,用户答完该题,进入下一题,计时器接续上一题的结束时间继续计时: 2.用户可以跳出答题界面, ...
- bzoj3942——2016——3——15
题目大意: 3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 220 Solved: 11 ...
- 10-instanceof
在Java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例 package com.example; class A{ void tell1(){ System.out.prin ...
- css3实战版的点击列表项产生水波纹动画
1.html+js: <!DOCTYPE html><html><head lang="en"> <meta charset=&qu ...
- 2.3. 实体(Core Data 应用程序实践指南)
Entity 实体,就像是数据表的映射类.用实体就可以创建托管对象了. 实体需要做的就是设置实体名称.实体属性及属性数据类型. 还可以根据实体配置NSManagedObject的子类(可选),创建子类 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)&&莫对算法
这里跟曼哈顿最小生成树没有太大的关系. 时间复杂度证明: [BZOJ2038 小Z的袜子 AC代码] 排序方式: 第一关键字:l所在的块: 第二关键字:r从小到大. #include<cstdi ...
- origin中把多个拟合曲线放在一张图
双击其中一个.或者New一个graph.这里直接双击其中一个图. 右键,找到layer contents. 可以看到,一个scatter配一个polynomial fit line.把剩下的B,C,D ...
- StackView的功能和用法
StackView也是AdapterViewAnimator的子类,它也用于显示Adapter提供的系列View.SackView将会以“堆叠(Stack)”方式来显示多个列表项. 为了控制Stack ...
- ImageView及其子类(三)
实例:使用QuickContactBadge关联联系人 QuickContactBadge继承了ImageView,因此它的本质也是图片,也可以通过android:src属性指定他显示的图片 ...