STL函数库大全

【pair】

//pair的一些用法
pair<int,int> p1;//定义一个pair类型的变量,类似与结构体
p1=make_pair(4,5);//制造一个pair,把它的值赋值给p1
pair<int,int> p2(3,4);//定义一个pair类型的变量p2,把(3,4)赋值给p2
p2=p1;//把p1的值赋给p2
cout<<make_pair(3,4).first<<endl;//制造一个pair,输出它的第一个类型的数
cout<<make_pair(3,4).second<<endl;//制造一个pair,输出它的第二个类型的数
sort(p1+1,p1+n+1);//排序,先用第一关键字比较,再用第二关键字比较
//结构体不能嵌套,pair可以嵌套
#include<bits/stdc++.h>
using namespace std;
void setbook(pair<int,pair<int,string> > &s)//这里的&s表示s的地址,改变s的值,如果改为s则不改变s的值
{
	s.first=3;
	s.second.first=4;
	s.second.second="World";
}
int main()
{
	pair<int,pair<int,string> > s;//pair的嵌套
	s.first=1;
	s.second=make_pair(2,"Hello");
	setbook(s);
	cout<<s.first<<endl;
	cout<<s.second.first<<endl;
	cout<<s.second.second<<endl;
	return 0;
}

【vector】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
vector<int> p1;
int main()
{
	for(int i=1;i<=7;i++)
		p1.push_back(i*i*i*i%17);//在p1后面插入一个7个伪随机数 
		
	cout<<p1.size()<<" "<<p1.capacity()<<endl;//输出p1的长度和它占用的内存 
	
//	vector<int>t(p1);
//	swap(t,p1);

	p1.shrink_to_fit();//删除多余的空间 
	
	cout<<p1.size()<<" "<<p1.capacity()<<endl; //输出p1的长度和它占用的内存 
	
	cout<<p1.max_size()<<endl; //理论上可以存储的最大元素个数
	
	p1.clear();//将p1数组清空
	
	if(p1.empty())//判断数组是否为空 
		cout<<"Impossble"<<endl;
		
	cout<<p1.capacity()<<endl;//p1实际占用的内存 
	
	p1.reserve(20);//重新设计内存 
	
	cout<<p1.capacity()<<endl;
	
	for (int i=1;i<=9;i++)
		p1.insert(p1.begin(),1,i*i*i*i%19);
		
	for (const int&tmp:p1)
		cout<<tmp<<' ';
	cout<<'\n';

	cout<<p1.front()<<'\n'; //返回第一个元素 
	p1.erase(p1.begin());
	
	cout<<p1.front()<<'\n';
	p1.erase(p1.begin(),p1.begin()+5);
	
	cout<<p1.front()<<'\n';
	p1.pop_back();//删除最后一个元素 
	
	p1.push_back(4);//在最后插入一个元素 
	
	p1.resize(20);//将p1填满20个位置 
	
	pair<int,int> z[11];
	for (const auto& tmp:z)	//遍历z
		cout<<tmp.first<<' '; 
	cout<<endl;
	
	int p2=3;
	p1.insert(p1.end(),p2);//插入p2的备份 
	
	for (const int&tmp:p1)//用迭代器的遍历方式 
		cout<<tmp<<' ';
	cout<<endl;
	
	for (int i=0;i<p1.size();i++)//用下标遍历 
		cout<<p1[i]<<' ';
	cout<<endl;
	
	sort(p1.begin(),p1.end());//sort快排 
	
	for (int i=0;i<p1.size();i++)
		cout<<p1[i]<<' ';
	cout<<endl;
	
	stable_sort(p1.begin(),p1.end());
	
	for (int i=0;i<p1.size();i++)//stable_sort归排 
		cout<<p1[i]<<' ';
	cout<<endl;
	
	vector<int>::iterator it;
	for (it=p1.begin();it!=p1.end();it++)//用迭代器的遍历方式 
		cout<<*it<<' ';
		
	vector<vector<int>> p3(9,vector<int>(9)); 
	vector<vector<int>> p4(9);
	
	for (auto& temp:p4)
		temp.reserve(9);
	return 0;
}

【map】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
	map<string,int> Map;//定一个以string为键值的map数组 
	
	Map["a"]=1;//把1赋值给下标为"a" 
	
	Map.insert(map<string,int>::value_type("b",2));
	Map.insert(pair<string,int>("c",114));
	Map.insert(make_pair<string,int>("d",4));
	//向map插入一个元素,第一个为键值,第二个为值
	
	cout<<Map["a"]<<' '<<Map["b"]<<' '<<Map["c"]<<' '<<Map["d"]<<'\n';
	Map.insert(make_pair("b",3));
	//map的insert只能插入一个,重复插入为无效操作 
	
	Map["a"]=2;
	//赋值与插入不同,可以重复替换 
	
	cout<<Map["a"]<<' '<<Map["b"]<<' '<<Map["c"]<<' '<<Map["d"]<<'\n';
	int x=Map["c"];
	cout<<x<<'\n';
	int y=Map["ccccc"];
	cout<<x<<'\n';
	//当它不能够找到这个值时,则自动插入一个,值为初始化
	
	map<string,int>::iterator it; 
	int key;
	it=Map.find("c");
	if (it!=Map.end())//在map中如果找不到返回map.end()
		key=it->second;//表示key等于这个键值 
		
	cout<<key<<' '<<Map["c"]<<'\n';
	
	bool i_t;
	i_t=Map.count("c");
	//如果"c"在Map中返回1,否则返回0 
	
	if (i_t) cout<<"yes\n";
	else cout<<"no\n";
	for(it=Map.begin();it!=Map.end();it++)
		cout<<it->first<<' '<<it->second<<'\n';
	//使用了迭代器i_t分别指向map的键值和实际值
	// first为键值, second为实际值 
	
	cout<<'\n';
	for(const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
	//当把迭代器这样用时,它是一个具体的值,即一个pair类型 
	
	cout<<'\n';
	map<int,string> Map2;
	Map2[1]="a";
	Map2[2]="b";
	Map2[3]="c";
	for(int i=1;i<=3;i++)
		cout<<Map2[i]<<' ';
	cout<<'\n';
	//只用但我们的键值时可以枚举的时候我们才可以这样遍历
	//例如string类型就不可以
	
	map<string,int>::iterator i__t;
	i__t=Map.find("a");
	Map.erase(i__t);
	//使用迭代器删除,先找到为键值为"a"的值,并且把它删除
	 
	for (const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
		
	int n=Map.erase("b");
	if (n)cout<<"yes\n";
	else cout<<"no\n";
	
	for (const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
	
//	forward_list 单向链表,基本没啥用 
//	unordered_+容器 ,就是用hash来存贮 
	map<string,int>::iterator i,j;
	i=Map.begin();
	j=Map.end();
	i++;
	j++;
	Map.erase(i,j); //删去i,j之间的区间 
	return 0;
}

【set】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main(){
	set<int> s;
	//定义一个set类型的s
	//set中的操作与map相似,但是set中的元素是自动排好序的 
	
	set<int>::iterator iter;
	for(int i=1;i<=7;i++)
		s.insert(i*i*i*i*i*i%23);
	//伪随机数,插入操作的时间复杂度为logn 
	
	for (iter=s.begin();iter!=s.end();iter++)
		cout<<*iter<<' ';
	cout<<'\n';
	//顺序遍历 
	
	set<int>::reverse_iterator iter2; 
	for (iter2=s.rbegin();iter2!=s.rend();iter2++)
		cout<<*iter2<<' ';
	cout<<'\n';
	//逆序遍历 
	
	for (const auto& tmp:s)
		cout<<tmp<<' ';
	cout<<'\n';
	//推荐,简单的遍历方式 
	
	set<int>::iterator t;
	t=s.lower_bound(16); 
	//找到set中第一个大于等于16的数 
	cout<<*t<<'\n';
	
	t=s.upper_bound(16);
	cout<<*t<<'\n';
	//找到set中第一个大于16的数 
	
	s.clear();
	//清空set中的数
	 
	if (s.empty())cout<<"yes";
	else cout<<"no";
	//判断是否为空 
	return 0;
}

【迭代器】

在这里插入图片描述

【算法】

在这里插入图片描述

【unique】

#include<bits/stdc++.h>
using namespace std;
int a[20000],n;
int main()
{
	cin>>n;
	for (int i=1;i<=n;i++)
		a[i]=i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i%97;
	//创造伪随机数 
	sort(a+1,a+n+1);
	//先从小到大排序 
	int t=unique(a+1,a+n+1)-a-1;//记住这里要减1 
	//去重,把重复的元素放到数组后面,返回去重后最后一个元素的值 
	for (int i=1;i<=t;i++)//输出去重后的a数组 
		cout<<a[i]<<' ';
	return 0;
 } 

【binary_search】

  • binary_search(a+1,a+n+1,x) 二分查找,返回值为bool,找到为真,否则为假。
#include<bits/stdc++.h>
using namespace std;
int a[1020];
int main()
{
	for (int i=1;i<=10;i++)
		a[i]=i*i*i*i*i*i%114514;
	//伪随机数
	for (int i=1;i<=10;i++)	
		cout<<a[i]<<' '; 
	cout<<'\n';
	if (binary_search(a+1,a+11,64))//在a中查找64,有则返回1,没有则返回0
		cout<<"yes\n";
	else cout<<"no\n";
	if (binary_search(a+1,a+11,65))//在a中查找65,有则返回1,没有则返回0
		cout<<"yes\n";
	else cout<<"no\n";
	return 0;
 } 

【next_permutation】

  • next_permutation(a+1,a+n+1) 生成全排列a的下一个全排列,需确保数组a是一个全排列
#include<bits/stdc++.h>
using namespace std;
int a[1020];
int main()
{
	for (int i=1;i<=10;i++)
		a[i]=i;
	for (int i=1;i<=10;i++)
		{
			next_permutation(a+1,a+11);//生成下一个全排列
			for (int j=1;j<=10;j++)
				cout<<a[j]<<' ';
			cout<<endl;
		}
	return 0;
 } 

【list】

Lst1.assign() 给list赋值 
Lst1.back() 返回最后一个元素 
Lst1.begin() 返回指向第一个元素的迭代器 
Lst1.clear() 删除所有元素 
Lst1.empty() 如果list是空的则返回true 
Lst1.end() 返回末尾的迭代器 
Lst1.erase() 删除一个元素 
Lst1.front() 返回第一个元素 
Lst1.get_allocator() 返回list的配置器 
Lst1.insert() 插入一个元素到list中 
Lst1.max_size() 返回list能容纳的最大元素数量 
Lst1.merge() 合并两个list 
Lst1.pop_back() 删除最后一个元素 
Lst1.pop_front() 删除第一个元素 
Lst1.push_back() 在list的末尾添加一个元素 
Lst1.push_front() 在list的头部添加一个元素 
Lst1.rbegin() 返回指向第一个元素的逆向迭代器 
Lst1.remove() 从list删除元素 
Lst1.remove_if() 按指定条件删除元素 
Lst1.rend() 指向list末尾的逆向迭代器 
Lst1.resize() 改变list的大小 
Lst1.reverse() 把list的元素倒转 
Lst1.size() 返回list中的元素个数 
Lst1.sort() 给list排序 
Lst1.splice() 合并两个list 
Lst1.swap() 交换两个list 
Lst1.unique() 删除list中重复的元素

函数原文链接:https://blog.csdn.net/fanyun_01/article/details/56881515

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int nxt,dt;
}a[100010];
int head;
int find(int value)//手写函数
{
	int p=head;
	while(a[p].nxt!=0)
	{
		if (a[p].nxt==value)
			return p;
		p=a[p].nxt;
	}
	return 0;
}
void insert(int nw,node &pre)//手写函数
{
	
	a[nw].nxt=pre.nxt;
	pre.nxt=nw;
}
void delet(node &delnode,node &pre)//手写函数
{
	pre.nxt=delnode.nxt;
}
bool cmp(int x)
{
	if (x<=0)return true;
	else return false;
}
int main()
{
	list<int> l; 
	l.push_back(1);
	l.push_front(2);
	for (const auto&tmp:l)
		cout<<tmp<<' ';
	cout<<'\n';
	l.pop_back();
	l.pop_front();
	if (l.empty())
		cout<<"yes\n";
	else cout<<"no\n";
	list<int> l2(3);//创建三个空的元素 
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	list<int> l3(3,2);//创建三个为二的元素 
	for (const auto&tmp:l3)
		cout<<tmp<<' ';
	cout<<'\n';
	l3.assign(3,4);//为l3赋值 
	for (const auto&tmp:l3)
		cout<<tmp<<' ';
	cout<<'\n';
	l2.splice(l2.begin(),l3);//把l3合并在l2前面,合并之后l3为空 
	l2.merge(l);//把l合并在l2后面,合并之后l为空 
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	l2.remove(4);//把链表值为4的数全部删了 
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	for(int i=-102;i<102;i+=7)
		l2.push_back(i);
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	l2.remove_if(cmp);//把符合条件的元素删除 
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	l3.assign(3,8);
	l2.swap(l3);//把l2和l3的值交换 
	for (const auto&tmp:l2)
		cout<<tmp<<' ';
	cout<<'\n';
	cout<<l3.max_size()<<'\n';//l3理论上最大储存元素个数
	l2.unique();//直接删除list中重复的元素 
	for (const auto&tmp:l2)
		cout<<tmp<<' '; 
	l2.sort();//给list排序 
	return 0;
 } 
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值