机试准备(四)

C++标准模板库(STL)的介绍

1.vector

#include<vector>
using namespace std;

初始化:
vector<int>  number;//变长一维数组,长度根据需要自动改变   
vector<int> v[100];//变长二位数组,v[0]~v[99]每一个都是一个vector容器。
常用函数
number.push_back(1);//在vector后面添加元素1
number.pop_back();//删除vector的末尾元素
number.size();//获得vector中的元素个数
number.clear();//清空vector中的所有元素
number.insert(it,x);//将元素x插入到迭代器it位置下
	number.insert(number.begin() + 2, -1);
number.erase(it);//删除迭代器it处的元素
number.erase(first,second);//删除[first,second)处的元素
	number.erase(number.begin() + 3);
number[3];//取位置3上面的元素

常用于用邻接表存储图

练习:
http://codeup.cn/contest.php?cid=100000596

2.set

include<set>
using namespace std;
set<int> s;
set<int> a[100];

set只能通过迭代器来访问,set内的元素自动递增排序并且去除重复。
set<int> ::iterator it;

常用函数
s.insert(1);//将1插入set中
s.find(2);//在set中查找2
s.erase(it);//删除单个元素,it为迭代器
s.erase(first,second);//删除[first,second)处的元素
	s.erase(s.find(2));
s.size();
s.clear();

练习:
http://codeup.cn/contest.php?cid=100000597

3.string

#include<string>
using namespace std;

string str = "abcd";

#include<iostream>
cin>>str;
cout<<str;
printf(“%s”,str.c_str());//使用c_str()变为字符数组

string :: iterator it;

常见操作
+ - 拼接字符串
==,>,<,>=,<=,!=,直接比较字典序
str.length(); || str.size();
str.insert(3,str2);
str.erase(it);
str.erase(first,second);//删除[first,second)处的元素
str.clear();
str.substr(pos,len);
str.find(str2);//返回str2第一次出现的位置,若没有,返回一个常数
	str.find(str2,pos);
str.replace(pos,len,str3);//将str从pos位开始,长度为len的子串替换为str3

练习:
https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872

4.map

#include<map>
using namespace std;

map<键类型,值类型> mp;
例:map<char,int> mp;
map['c'] = 30;

通过迭代器访问:
for(map<char,int> :: iterator it = mp.begin();it!=map.end();it++){
	printf("%c %d",it->first,it->second);//显示,以键从大到小排序,因为map内部是用红黑树实现的。
}


常用的函数:
1. find(key):返回迭代器,
map<char,int> :: iterator it = mp.find('c');
printf("%c %d",it->first,it->second);
2. erase():
mp.erase(it);删除需要删除元素的迭代器。
mp.erase(key);
mp.erase(it,mp.end());//删除一个区间内的元素
3. size():获得映射的对数。
4. clear():清空map中的所有元素
5. 判断大整数的时候,可以将大整数用字符串存储,然后使用map

5.queue

#include<queue>
queue<int> q;

q.front();//访问队头元素
q.back();//访问队尾元素
q.push(x);
q.pop();//队首出队
q.empty();//检测是否为空,使用front()和back()前判断
q.size();

练习:
http://codeup.cn/contest.php?cid=100000600

6.priotity_queue

优先队列
#include<queue>
using namespace std;
priority_queue<int> q;
q.push(x);
q.top();
q.pop();
q.empty();//使用top前判断
q.size();
优先级的设置
1.对于基本数据类型
	priority_queue<int,vector<int>,greater<int>> q;//总把最小元素放在队首(数字小优先级大),将greater换为less,则总把最大元素放在队首(数字大优先级大)。

结构体运算符重载P224

练习:
http://codeup.cn/contest.php?cid=100000601

7.stack

#include<stack>
using namespace std;
stack<int> s;
s.push(x);
ss.top();
s.pop();
s.empty();
s.size();

练习:
http://codeup.cn/contest.php?cid=100000602

8.pair

#include<utility>//注意了哟
using namespace std;
pair<string,int> p;
p.first = "hahh";
p.second = 3;
p.make_pair("xixi",6);
p = pair<string,int>("heiyo",2);

比较:>,<,==,!=,<=,>= :只有当first相等时才比较second大小

常作为map插入键值对;
#include<map>
map<string,int> mp;
mp.insert(make_pair("haha",7));

练习:
http://codeup.cn/contest.php?cid=100000603

9.algorithm头文件下的常用函数

max(x,y);
min(x,y);
abs(x);
swap(x,y);
reverse(it1,it2);//[it1,it2)内元素反转
next_permutation();//给出一个序列在全排列中的下一个序列
fill();
	例如:int a[5] = {1,2,3,4,5};
	fill(a,a+3,233);//[a,a+3)
	现在数组a:233,233,233,4,5;

10.sort

#include<algorithm>
using namespace std;

sort(首元素地址,尾元素地址的下一个地址,比较函数(非必需))  //默认从小到大,对int,double,char等可比元素进行排序

比较函数:cmp
用于结构体
struct node{
	int x;
	int y;
}
//先按照x从大到小进行排序,然后当x相等时,按照y从小到大进行排序
bool cmp(node a,node b){
	iif(a.x != b.x) return a.x > b.x;
	else return a.y < b.y
}


在stl标准容器中,只有vector,string ,deque才可以使用sort

看vector的排序
添加
#include<vector>
然后main中如下:
vector<int> v;
v.push_back(3);
v.push_back(1);
v.push_back(2);
sort(v.begin(),v.end());//排好序 1 2 3


看string的排序
#include<string>
string str[3] = {"bbbb","cc","aaa"};
sort(str,str+3);//输出 aaa bbbb cc

//如果需要按照字符串的长度从大到小进行排序,设置比较函数
bool cmp(string a,strng b){
	return a.length() > b.length();
}
sort(str,str+3,cmp);

11,lower_bound() 和upper_bound()

lower_bound(first,last,val);//寻找在数组或容器的[fisrt,last)范围内第一个值大于等于val元素的位置,数组返回指针,容器返回迭代器。 
upper_bound(first,last,val);//寻找在数组或容器的[fisrt,last)范围内第一个值大于val元素的位置,数组返回指针,容器返回迭代器。 区别lower
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值