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 ...
随机推荐
- Python 3 iter函数用法简述
Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel)这种形式一般较少使用 ...
- java基础编程练习
1.编写程序实现对给定的 4 个整数从大到小的顺序排列. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- 【Linux基础】文件处理实例
1.文件拆分 //每4000行拆分一个文件 epms_t_ep_fx_stl_xy_20190129.dat 2.行处理 //查找第二列为711611且第三列为711100记录,打印行号和整行数据 a ...
- 创建 tomcat 服务的镜像
如何设计 Tomcat 的 Dockerfile $ sudo docker search tomcat |wc -l 285 在 dockerhub 上搜索与 tomcat 相关的镜像,有如此之多的 ...
- Mysql 数据库设置三大范式 数据库五大约束 数据库基础配置
数据库设置三大范式 1.第一范式(确保每列保持原子性) 第一范式是最基本的范式.如果数据库表中的所有字段值都是不可分解的原子值,就说明该数据库满足第一范式. 第一范式的合理遵循需要根据系统给的实际需求 ...
- Leetcode:0027
Leetcode:0027 题目:给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 ...
- ubantu下Navicat乱码的问题
在官网下载的最新版的Navivat12出现的乱码情况 解决方法:Navicat的文件夹中找到start_navicat用vim编辑,在export LANG=“en_US.UTF-8”这句话改为exp ...
- Python学习笔记(3)-字符串
创建字符串 一对单引号或双引号 >>> 'hello world' 'hello world' >>> "hello world" 'hello ...
- ubuntu1604安装谷歌游览器
https://www.linuxidc.com/Linux/2016-05/131097.htm 在终端中依次运行如下命令: sudo add-apt-repository ppa:a-v-shko ...