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 ...
随机推荐
- js获取select选中的内容
### 获取select选中的内容 js获取select标签选中的值 var obj = document.getElementById("selectId");//获取selec ...
- SpringCloud之初识Zuul(网关)---动态路由,权限验证
通过前面的学习,使用Spring Cloud实现微服务的架构基本成型,大致是这样的: 我们使用Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现:而服务间通过 ...
- Photoshop怎么破解?PS怎么破解?
Photoshop和PS这两个软件可以说是十分常见的图片处理软件了,Photoshop主要处理以像素所构成的数字图像进行图片编辑工作,而PS就更加强大了,它有很多功能,在图像.图形.文字.视频.出版等 ...
- 1. 路过面了个试就拿到2个offer。是运气吗?
路过随便面个试就拿到2个offer.是运气吗? #复习很重要#看看面试问的问题,再瞧瞧师兄的学习态度,你就明白 机会为何总与你擦肩而过了.[玫瑰] 以下是我和师兄的聊天记录,你会几个?
- jvisualVM的使用
jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程分析... jvisualvm已经被集成在jdk1.6以上的版本中(不是jre).自身运行需要最低jdk1.6版本, ...
- java中伪共享问题
伪共享(False Sharing) 原文地址:http://ifeve.com/false-sharing/ 作者:Martin Thompson 译者:丁一 缓存系统中是以缓存行(cache l ...
- Netty中ByteBuf 的零拷贝
转载:https://www.jianshu.com/p/1d1fa2fe1ed9 此文章已同步发布在我的 segmentfault 专栏. 根据 Wiki 对 Zero-copy 的定义: &quo ...
- Linux的常见问题解答和管理技巧
Linux的常见问题解答和管理技巧 一. 如何建立多用户 提醒大家一句,别一直使用root用户,因为root用户在系统中有着至高无上的权力,一不小心就可能破坏系统.比如我们想删除/temp目录下的文件 ...
- JAVA序列化和反序列化XML
package com.lss.utils; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.Bu ...
- Integer判断大于 == 127时的坑
在一次判断返回Interger类型的code, 用==结果, 没进去 Integer的值在-128到127时,Integer对象是在IntegerCache.cache产生,会复用已有对象,也就是说 ...