初涉算法——STL初步
一、头文件<algorithm>
①sort函数
- sort使用数组元素默认的大小比较运算符进行排序,只有在需要按照特殊依据进行排序时才需要传入额外的比较函数;
- sort可以给任意对象排序(不一定是内置类型,由此可见sort是模板函数),前提是类型需要定义“小于”运算符,或者在排序时传入一个“小于”函数;
- 排序对象若存在普通数组中,则用sort(a, a+n)的方式调用;
- 排序对象若存在vector中,则用sort(v.begin(), v.end())。
#include<algorithm>
const int maxn = ;
int main()
{
int n, a[maxn];
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
}
②lower_bound函数
- 查找大于或者等于x的第一个位置
- 可以在用sort函数排好序后再使用lower_bound
#include<algorithm>
const int maxn = ;
int main()
{
int n, a[maxn];
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
scanf("%d",&x);
int p = lower_bound(a, a+n, x) - a; //在已排序数组a中寻找x
if(a[p]==x)
printf("%d found at %d\n", x, p+);
}
二、头文件<vector>
①特点:它把一些常用操作“封装”在了vector类型内部(例如:函数size()可以读取其大小)。
②区别于数组:无需另外用一个变量(maxn)指定元素个数。
③优点:可以直接赋值,还可以作为函数的参数或者返回值。
vector是一个模板类:
int main()
{
vector<int> a; //声明一个不定长int型数组a
a.push_back(); //在尾部添加元素1
a.pop_back(); //在尾部删除元素
cout<<a.size(); //输出a的大小
a.resize(); //修改a的大小为4
a.clear(); //清空a,此时a的大小为0
bool isEmpty = a.empty(); //a为空,则isEmpty被赋值为true
}
三、头文件<set>
定义:set是数学上的集合——每个元素最多只能出现一次。
特点:和sort一样,自定义类型也可以构造set,但必须定义“小于”运算符。
性质:set中元素已从小到大排好序。
题目:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写。
#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std; set<string> dict; //声明string集合
string s, buf; int main()
{
while(cin >> s) { //注意:string已经定义了"小于"运算符,可直接使用set保存单词集合
for(int i = ; i < s.length(); i++) //利用了set的性质,一个for循环即可从小到大遍历所有元素
if(isalpha(s[i])) //判定s[i]是否为字母
s[i] = tolower(s[i]); //全都转为小写
else
s[i] = ' ';
stringstream ss(s);
while(ss >> buf)
dict.insert(buf);
}
for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)//利用了set的性质,一个for循环即可从小到大遍历所有元素
cout << *it << "\n"; //迭代器it的用法之一
return ;
}
四、头文件<map>
定义:map就是从键(key)到值(value)的映射。
特点:重载了[]运算符,map像是数组的“高级版”。
举例:用map<string,int> month_name来表示“月份名字到月份编号”的映射,然后用month_name["July"]=7这样的方式来赋值。
题目:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排而得到输入文本中的另外一个单词。字母不区分大小写。
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; map<string,int> cnt;
vector<string> words; string repr(string s) //将单词s进行标准化
{
string ans = s;
for(int i = ; i < ans.length(); i++)
ans[i] = tolower(ans[i]);
sort(ans.begin(), ans.end());
return ans;
} int main() {
int n = ;
string s;
while(cin >> s) {
if(s[] == '#') break;
words.push_back(s);
string r = repr(s);
if(!cnt.count(r)) //set和map都支持insert、find、count和remove操作,并且可以按照从小到大的顺序循环遍历其中的元素
cnt[r] = ;
cnt[r]++;
}
vector<string> ans;
for(int i = ; i < words.size(); i++)
if(cnt[repr(words[i])] == )
ans.push_back(words[i]);
sort(ans.begin(), ans.end());
for(int i = ; i < ans.size(); i++)
cout << ans[i] << "\n";
return ;
}
小结:set和map都支持insert、find、count和remove操作,并且可以按照从小到大的顺序循环遍历其中的元素。此外,map还提供了“[]”运算符,使得map可以像数组一样使用。
五、头文件<stack>
- 定义:stack<int> s
- 入栈:push()
- 出栈:pop()
- 取栈顶元素:top()
题目:Uva12096
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) typedef set<int> Set;
map<Set,int> IDcache; //把集合映射成ID
vector<Set> Setcache; //根据ID取集合 //查找给定集合x的ID。如果找不到,分配一个新ID
int ID (Set x) {
if (IDcache.count(x)) return IDcache[x];
Setcache.push_back(x); //添加新集合
return IDcache[x] = Setcache.size() - ;
} int main () {
int T;
cin >> T;
while(T--) {
stack<int> s; //题目中的栈
int n;
cin >> n;
for(int i = ; i < n; i++) {
string op;
cin >> op;
if (op[] == 'P') s.push(ID(Set()));
else if (op[] == 'D') s.push(s.top());
else {
Set x1 = Setcache[s.top()]; s.pop();
Set x2 = Setcache[s.top()]; s.pop();
Set x;
if (op[] == 'U') set_union (ALL(x1), ALL(x2), INS(x));
if (op[] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));
if (op[] == 'A') { x = x2; x.insert(ID(x1)); }
s.push(ID(x));
}
cout << Setcache[s.top()].size() << endl;
}
cout << "***" << endl;
}
return ;
}
六、头文件<queue>
- 定义:queue<int> s
- 入队:push()
- 出队:pop()
- 取队首元素:front()
【优先队列】
区别于队列:先出队列的元素是队列中优先级最高的元素。
声明:priority_queue<int> pq //pq是一个“越小的整数优先级越低的优先队列”
取队首元素:top()
注:自定义类型也可以组成优先队列,但必须为每个元素定义一个优先级。
初涉算法——STL初步的更多相关文章
- 变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...
- STL非变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1394600460.html 原创:ST ...
- 算法竞赛入门经典5.2 STL初步
1. 排序和检索,学会使用sort排序,以及low_bound函数 Raju and Meena love to play with Marbles. They have got a lot of m ...
- (STL初步)不定长数组:vector
STL是指C++的标准模板库.(存储着一些常用的算法和容器) vector是一个不定长数组.它把一些常用的操作”封装“在vector类型内部. 例如,a是一个vector.1对元素的操作有,可以用a. ...
- 51nod 1785 数据流中的算法 | STL的应用
51nod 1785 数据流中的算法 题面 动态求平均数.方差.中位数. 题解 这道题的坑: 平均数在答案中是向下取整输出并在后面添加".00" 方差:平方的平均数减去平均数的平方 ...
- 【STL初步】不定长数组:vector + 集合:set + 映射:map
一.vector 为了节省空间,有时我们会使用动态数组vector. 定义动态数组 vector<类型名>变量名 vector<int>que //定义que为一个int类型的 ...
- BKDRHash算法的初步了解
字符串hash最高效的算法, 搜了一下, 原理是: 字符串的字符集只有128个字符,所以把一个字符串当成128或更高进制的数字来看,当然是唯一的 这里unsigned不需要考虑溢出的问题, 不过 ...
- 2020.2.27——STL初步
注:本文主要针对STL中的常用的操作进行总结 目录: 1.swap 2.sort 3.reverse 4.min,max(比较简单,暂且略过) 5._gcd 6.lower_bound &&a ...
- STL初步学习(queue,deque)
4.queue queue就是队列,平时用得非常多.栈的操作是只能是先进先出,与栈不同,是先进后出,与之后的deque也有区别.个人感觉手写队列有点麻烦,有什么head和tail什么的,所以说 STL ...
随机推荐
- 复习宝典之MyBatis
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第五章:MyBatis MyBatis是一个可以自定义SQL.存储过程和高级映射的持久层框架. 1)创建sqlsession的流程 my ...
- IDEA一直提示 错误: 找不到或无法加载主类
1.把http://repo1.maven.org/maven2...下载下来2.放到本地Manen仓库archetype 文件夹下3.设置IDEA Maven->Runner 界面的VM Op ...
- Ubuntu之C++开发环境的搭建
初学Linux,今天反复卸载与重装微软商店的Ubuntu好几次,终于解锁了在Ubuntu上搭建C++开发环境的正确姿势, 搭建了一个非常简单的开发环境:简单到什么地步呢?只是简单地配置了一下编辑器,安 ...
- 针对jquery的ajax中的参数理解
1. url 发送请求的地址.为空表示当前页. $.ajax({ type: "post", data: studentInfo, contentType: "appli ...
- ;(function($,window,document,undefined){})(jQuery,window,document)
;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...
- h5图片上传简易版(FileReader+FormData+ajax)
一.选择图片(input的file类型) <input type="file" id="inputImg"> 1. input的file类型会渲染为 ...
- mysql 主主架构,多入口 互为备份
,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入口,增加高可用.不过多主需要考虑自增长ID ...
- php的基础知识(三)
12.函数: 函数的功能: 定义:在真实的项目开发过程中,有些代码会重复利用,我们可以把它提出来,做成公共的代码,供团队来使用,这个我们封装的代码段,就是函数(功能). 优点: 1.提高代码的利用率. ...
- Struts2+Datagrid表格显示(可显示多表内容)
概述 最近学到EasyUI的Datagrid数据网格,然后就做了一个小例子,中间层利用Struts2来完成,DAO层用的是Hibernate. 数据库 数据库涉及到stuednt(name,noid, ...
- spark----词频统计(一)
利用Linux系统中安装的spark来统计: 1.选择目录,并创建一个存放文本的目录,将要处理的文本保存在该目录下以供查找操作: ① cd /usr/local ②mkdir mycode ③ cd ...