一、头文件<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初步的更多相关文章

  1. 变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...

  2. STL非变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1394600460.html 原创:ST ...

  3. 算法竞赛入门经典5.2 STL初步

    1. 排序和检索,学会使用sort排序,以及low_bound函数 Raju and Meena love to play with Marbles. They have got a lot of m ...

  4. (STL初步)不定长数组:vector

    STL是指C++的标准模板库.(存储着一些常用的算法和容器) vector是一个不定长数组.它把一些常用的操作”封装“在vector类型内部. 例如,a是一个vector.1对元素的操作有,可以用a. ...

  5. 51nod 1785 数据流中的算法 | STL的应用

    51nod 1785 数据流中的算法 题面 动态求平均数.方差.中位数. 题解 这道题的坑: 平均数在答案中是向下取整输出并在后面添加".00" 方差:平方的平均数减去平均数的平方 ...

  6. 【STL初步】不定长数组:vector + 集合:set + 映射:map

    一.vector 为了节省空间,有时我们会使用动态数组vector. 定义动态数组 vector<类型名>变量名 vector<int>que //定义que为一个int类型的 ...

  7. BKDRHash算法的初步了解

    字符串hash最高效的算法,  搜了一下,  原理是: 字符串的字符集只有128个字符,所以把一个字符串当成128或更高进制的数字来看,当然是唯一的 这里unsigned不需要考虑溢出的问题,  不过 ...

  8. 2020.2.27——STL初步

    注:本文主要针对STL中的常用的操作进行总结 目录: 1.swap 2.sort 3.reverse 4.min,max(比较简单,暂且略过) 5._gcd 6.lower_bound &&a ...

  9. STL初步学习(queue,deque)

    4.queue queue就是队列,平时用得非常多.栈的操作是只能是先进先出,与栈不同,是先进后出,与之后的deque也有区别.个人感觉手写队列有点麻烦,有什么head和tail什么的,所以说 STL ...

随机推荐

  1. 19-3-4 Python进制转换;bool str int三者之间的转换;字符串的索引,切片;for循环的使用

    进制转换: 二进制转十进制:  0010 1111 = 1*2**0+1*2**1+1*2**2+1*2**3+1*2**5 十进制转换二进制: 用十进制数除2逆序取余 --->101010 布 ...

  2. CentOS7 使用chrony搭建集群中的时间同步服务

    一.集群环境: 系统:CentOS7-minimal 集群中的两台主机ip:10.132.226.103/24  10.132.226.104/24 二.CentOS7中时间相关命令timedatec ...

  3. JavaScript 时间对象 date()

    getYear() 获得的是距离1900年过了多少年 var d = new Date(); document.write(d+"<br />"); document. ...

  4. TinyMCE插件:RESPONSIVE filemanager 9 文件名统一格式化

    上传图片方法(filemanager/UploadHandler.php) 在上传图片的函数中查看,发现$file->name是一个完整的[文件名.后缀名],所以使用explode(),文件名和 ...

  5. html5 video获取当前时间和视频总时间长度

    html: <video id="video-active" class="video-active" width="640" hei ...

  6. 在WIN7下安装运行mongodb

    1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...

  7. AS5600磁编码器开发记录

    AS5600使用简介--(程序员版) -----------------本文由"智御电子"提供,同时提供范例教程,以便电子爱好者交流学习.---------------- 前言: ...

  8. Ruby中类的进阶(继承,private, public, protect)

    类中的public,protect,private public method class Point def test end end 这样定义的test方法就是一个public方法可以在类内外使用 ...

  9. 《JQuery常用插件教程》系列分享专栏

    <JQuery常用插件教程>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201719.html 文章 使用jquery插件实现图 ...

  10. java 代码块的执行顺序

    举一个实例程序: class HelloA { public HelloA(){ System.out.println("Hello A!父类构造方法"); } { System. ...