C++实现算法常用的STL---整理
algorithm
min(a,b)和max(a,b)
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
cout << max(9,4) <<endl; //9
cout << min(9,4) <<endl; //4
}
sort快排
#include<algorithm> //注意包含algorithm头文件
#include<iostream>
using namespace std;
int main()
{
int arr[] = {1,9,8,4,3,6,0,11};
int length = sizeof(arr) / 4; //快速排序
//sort的排序范围是[start, end),默认使用从小到大排序。
sort(arr, arr + 3); //只排序前3个
sort(arr + 2, arr + length); //排序第2个元素之后的元素。
sort(arr, arr + length); //排序整个数组
sort(arr, arr + length, greater<int>()); //从大到小排序整个数组
return 0;
}
binary_search二分查找
int arr[] = {0,3,5,7,10,15,19,20,22,24,27};
int length = sizeof(arr) / 4;
bool isFind = true;
isFind = binary_search(arr + 2, arr + length, 19); //在[start, end)中进行二分查找key
cout<< isFind << endl;
vector(数组)
#include<iostream>
#include<vector>
using namespace std; void printVector(vector<int>& v) {
for (vector<int>::iterator begin = v.begin(); begin != v.end(); begin++) {
cout << *begin << " ";
}
cout << endl;
}
int main() {
vector<int> v;
cout << "isEmpty:" << v.empty() << endl;
v.push_back(9); //在数组的最后添加一个元素
v.push_back(12);
v.push_back(15);
cout << "Size:" << v.size() << endl; // 3 v.pop_back(); //删除最后一个元素
printVector(v); //输出 9 12 cout << "v[1] = " << v.at(1) << endl; // v[1] = 12 v.insert(v.begin() + 1, 99); // 在第v[1]位置插入99元素
v.insert(v.begin() + 1, 5, 88); //从v[1]开始,插入5个88
printVector(v); //9 88 88 88 88 88 99 12 v.erase(v.begin()); //删除第i个数
printVector(v); //88 88 88 88 88 99 12 cout << "first:" << v.front() << endl; }
stack(栈)
#include<stack> //注意包含stack头文件
#include<iostream>
using namespace std;
int main()
{
stack<int> s; //声明stack中的类型,以及栈名称
s.push(3);
s.push(4); //入栈
int length = s.size(); //获取栈元素数量
int top = s.top(); //获取栈顶元素(不出栈)
s.pop(); //出栈
bool isEmpty = s.empty();
return 0;
}
queue(普通队列)
#include<iostream>
#include<queue> //注意包含queue头文件
using namespace std;
int main()
{
queue<string> q;
string s;
q.push("hello"); //入队
q.push("world"); cout<< q.front() << endl; //hello 获取队首元素值,但是不出队
cout<< q.back() << endl; //world 获取队尾元素值,但是不出队 q.pop(); //队首元素出队
cout<< q.front() << endl; //world
cout<< "size " << q.size() << endl;
cout<< "isEmpty " << q.empty() <<endl;
return 0;
}
deque(双向队列)
#include<iostream>
#include<deque>
using namespace std; void printDeque(deque<int> & dq) {
for (deque<int>::iterator begin = dq.begin(); begin != dq.end(); begin++) {
cout << *begin << " ";
}
cout << endl;
}
int main() {
deque<int> dq;
dq.push_back(5);
dq.push_back(6);
dq.push_front(8);
dq.push_front(7);
printDeque(dq); //7 8 5 6 dq.pop_back();
dq.pop_front();
printDeque(dq); // 8 5 cout << dq.front() << endl; // 8
cout << dq.back() << endl; // 5 dq.insert(dq.begin() + 1, 9);
printDeque(dq); //8 9 5 dq.insert(dq.begin(), 5, 88);
printDeque(dq); // 88 88 88 88 88 8 9 5
cout << "size:" << dq.size() << endl; //8
}
list(双向链表)
#include<iostream>
#include<list>
using namespace std; void printList(list<int> l){
list<int>::iterator p;
for (p = l.begin(); p != l.end(); p++) {
cout << *p << " ";
}
cout << endl;
}
int main(){
list<int> l;
l.push_front(6); //从左边入队
l.push_front(7);
l.push_back(8); //从右边入队
printList(l); //7 6 8 cout << l.front() << endl; //7 返回左边第一个元素的值(不删除元素)
cout << l.back() << endl; // 8 返回有边第一个元素的值(不删除元素)
cout << l.size() << endl; //返回元素的总个数 l.reverse(); //进行翻转
printList(l); //8 6 7 l.pop_front(); //删除左边第一个元素
l.pop_back(); //删除右边第一个元素
printList(l); //6 l.push_back(6);
printList(l); //6 6
l.remove(6); //删除值为6的所有元素
cout << l.empty() << l.size() << endl; //1 0 l.push_back(4);
l.push_back(1);
l.push_back(3);
l.sort(); //排序
printList(l); //1 3 4
}
set/multiset(集合)
multiset/set使用平衡二叉树的数据结构,插入和查找时间复杂度都是log n。
multiset和set的用法相同,只有一个区别:
1、multiset中可以出现重复的元素。
2、set中不会出现重复的元素,即使添加重复的元素,也会自动去重。
#include<iostream>
#include<set> //multiset和set都要包含set头文件
using namespace std;
int main()
{
int arr[10] = {5,1,2,4,6,4,3,5,8,8};//有重复的元素
int i;
multiset<int> ms; //创建一个空格multiset集合
for (i = 0; i < 10; i++) {
ms.insert(arr[i]);
} multiset<int>::iterator p; //声明一个迭代器,类似于指针
for (p = ms.begin(); p != ms.end(); p++) {
//ms.begin() 返回一个迭代器,指向multiset的第一个元素
//ms.end() 返回一个迭代器,指向multiset最后一个元素的后面一个位置
cout << *p << " "; //1 2 3 4 4 5 5 6 8 8
}
cout << endl;
int length = ms.size(); //集合中元素的数量
bool isEmpty = ms.empty(); // 集合是否为空
int cnt = ms.count(8); //计算一个数出现的次数
cout<< length << " " << isEmpty << " " << cnt << endl; // 10 0 2 //查找元素,如果找到的话,返回一个迭代器指向找到的元素。如果没有找到的话,就返回multiset中元素总个数size
p = ms.find(8);
if (*p != ms.size()) {
cout<< "found " << *p << endl; //8
ms.erase(*p); //删除集合中所有的8,不是只删除一个。
cout<< "after delete , the size is "<< ms.size() << endl;
} else {
cout<<"not found"<<endl;
}
return 0;
}
map/multimap(映射、字典)
map和multimap的都是使用hash算法。
区别在于,map中的key只能出现一次,而multimap可以出现很多次。
#include<iostream>
#include<map> //multimap和map都要包含set头文件
using namespace std;
int main()
{
map<string, string> m;
m["one"] = "hello";
m["two"] = "world";
m.insert(pair<string, string>("three", "C++")); bool isEmpty = m.empty();
int length = m.size();
string s = m["one"]; //找到的话,就返回对应的值
cout<< s <<endl; // hello
s = m["four"]; //未找到的话,就返回一个类型零值
cout<< s <<endl; //返回空字符串 map<string, string>::iterator p;
p = m.find("one");
cout<< p->second << endl; //输出one对应的值--> hello m.erase(p); //删除某个key
for (p = m.begin(); p != m.end(); p++) {
cout<< p->second << " "; // C++ world
} m.clear(); //清空map
return 0;
}
C++实现算法常用的STL---整理的更多相关文章
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
- .NET平台常用的框架整理
基于.NET平台常用的框架整理 DotNet | 2016-03-31 17:13 (点击上方蓝字,可快速关注我们) 来源:天使不哭 链接:http://www.cnblogs.com/hgmyz/p ...
- 基于.NET平台常用的框架整理<转载>
转载来自:http://www.cnblogs.com/hgmyz/p/5313983.html 基于.NET平台常用的框架整理 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大 ...
- linux 常用命令--------雪松整理
linux 常用命令--------雪松整理 博客: http://hi.baidu.com/quanzhou722/blog错误在所难免,还望指正!========================= ...
- 刷题常用的STL容器总结
本文归纳总结刷题常用到STL容器以及一些标准算法,主要包括: string.vector.map.pair.unordered_map.set.queue.priority_queue.stack,以 ...
- 基于.NET平台常用的框架整理(转)
基于.NET平台常用的框架整理 分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问 ...
- python算法常用技巧与内置库
python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...
- iOS 常用三方类库整理
iOS 常用三方类库整理 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://gi ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- mysql copy表或表数据常用的语句整理汇总
mysql copy表或表数据常用的语句整理汇总. 假如我们有以下这样一个表: id username password ----------------------------------- 1 a ...
随机推荐
- JavaScript -- 时光流逝(十一):DOM -- Document 对象
JavaScript -- 知识点回顾篇(十一):DOM -- Document 对象 (1) document.activeElement: 返回文档中当前获得焦点的元素. <!doctype ...
- 【2018.08.13 C与C++基础】网络通信:阻塞与非阻塞socket的基本概念及简单实现
一.前言 最近在做Matalb/Simulink与C/C++的混合编程,主要是完成TCP.UDP.SerialPort等常见通信方式的中间件设计,为Simulink模型提供数据采集及解析模块. 问题在 ...
- 百度云资源下载加速软件推荐:proxyee-down
百度云是个好东西(现在叫百度网盘不过我还是习惯叫百度云),2个T的免费容量可以存视频.软件包等各式文件,就是下载速度有点让人看不下去,不开会员的话就算你是百兆光纤还是量子通信都是被限速的,做为一个商业 ...
- Photoshop怎么破解?PS怎么破解?
Photoshop和PS这两个软件可以说是十分常见的图片处理软件了,Photoshop主要处理以像素所构成的数字图像进行图片编辑工作,而PS就更加强大了,它有很多功能,在图像.图形.文字.视频.出版等 ...
- 配置数据库方言——hibernate
RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...
- [matlab] 21.灰色预测、线性回归分析模型与最小二乘回归 (转载)
灰色预测的主要特点是只需要4个数据,就能解决历史数据少,序列的完整性以及可靠性低的问题,能将无规律的原始数据进行生成得到规律性较强的生成序列,易于检验 但缺点是只适合中短期的预测,且只适合指数级增长的 ...
- USB知识汇总
概述 通用串行总线(英语:Universal Serial Bus,缩写:USB)是连接计算机系统与外部设备的一种串口总线标准,也是一种输入输出接口的技术规范,被广泛地应用于个人电脑和移动设备等信息通 ...
- Thymeleaf3语法详解
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code Thymeleaf是Spring boot推荐使用的模版引擎,除此之外常见的还有F ...
- 【ES6】函数
函数默认值问题 在ES6之前,不能直接为函数指定默认值,但是ES6允许为函数的参数设置默认值 之前实现方式 function log(x, y) { y = y || 'World'; console ...
- mybatis error
2018-08-02 14:01:18.021 WARN org.apache.catalina.loader.WebappClassLoaderBase Line:179 - The web app ...