算法竞赛中常用的STL容器

目录

前言

一、STL是什么?

二、竞赛中常见STL的容器

1.string类

2.vector 

3.stack

4.queue

5.priority_queue

6.set

7.map

8.pair

总结


前言

在我们的算法竞赛中,为了节约时间,我们不可能去手撕一个数据结构,所以在赛场上我们就要引入STL,以便我们节约时间,能有更多的时间处理更复杂的算法。


一、STL是什么?

STL(Standard Template Library)——每个字母分别代表标准、模板、库

STL不是面向对象的编程——而是一种不同的编程模式——通用编程技术,它是C++标准库的重要组成部分。它不仅是一个可复用的组件库,而且是一个包含数据结构与算法的软件框架。

二、竞赛中常见STL的容器

1.string类

顾名思义,string是c++中的一个类,严格意义上来说它并不算在STL模板里,string本质就是字符数组。那么废话少说,直接看代码。

#include 
#include 
using namespace std;
int main()
{
  string s0;//定义一个string类型的变量
  string s1("hello world");//将hello world赋值给s1
  //string s1;
  //cin>>s1;我们也可以这样赋值
  string s2(s1);//创建s2并将s1赋值给s2
  string s3(s1, 5, 3);//创建s3,并将s1下标为5的数加其后2个数赋值给s3
  string s4(s1, 5, 10);//同上
  string s5(s1, 5);//

  cout << s0 << endl;
  cout << s1 << endl;
  cout << s2 << endl;
  cout << s3 << endl;
  cout << s4 << endl;
  cout << s5 << endl;

  string s6(10, '#');//创建s6,并产生10个#
  cout << s6 << endl;

  s0 = s6;//可以直接用=赋值
  cout << s0 << endl;
  return 0;
}

代码的运行结果自己去编译器上查.STL仅仅只是方便了构造一个字符数组?

当然不是,还有许许多多的接口,我们列举几个常用的

#include 
#include 
using namespace std;
int main()
{
  //string s1="122334";
  //cout<

string相对的用的较少,不做重点描述

2.vector 

vector也叫数组,它其实和我们数据结构中的顺序表类似,废话少说直接上代码

#include 
#include 
int main()
{
   //vectorarr;创建arr类型是int形
   //vectorarr1;double形
   //vector>arr2(5,vector>(6,10);//5行6列的数组,并赋值
   //相当于int arr2[5][6]
   vectorarr;
   arr.push_back(1);
   arr.push_back(2);
   arr.push_back(3);
   arr.push_back(4);//尾插
   cout << arr.empty() << endl;
   cout << arr.size() << endl;//判空和计算大小
   arr.clear();置空
   arr.pop_back();//尾删
   cout << arr.size() << endl;

   arr.resize(5, 3);//缩容或者扩容,如果size>5则会缩容,如果size<5,则会扩容

   //遍历vector
   //有3种方法(1)下标+【】(2)范围for (3)迭代器
   for(int i=0;i::itereator it=arr.begin();it!=arr.end();it++)
{
   cout<<*it<

运行结果自己用编译器查看

3.stack

stack,也就是数据结构中的栈,具有先进后出的特点,直接看代码

#include 
#include 
using namespace std;
int main()
{
  stackstk;
  stk.push(1.0);//注意这里不是push_back
  stk.push(2.0);
  cout<vec;
 //vec.push_back(1.0);
 //vec.push_back(2.0);
 //cout<

4.queue

queue,也就是数据结构中的队列,具有先进先出的特点,直接看代码

#include 
#include 
using namespace std;
int main()
{
  queueque;
  que.push(1);//和stack一样
  que.push(2);
  que.push(3);
  
  cout<

5.priority_queue

priority_queue,是数据结构中的优先队列或者是堆,直接看代码

#include 
#include 
using namespace std;
int main()
{
  priority_queuepque;//大顶堆
  pque.push(1);
  cout<,greater>pque;//小顶堆
  priority_queue, greater>pque1;//小顶堆
  pque1.push(1);
  pque1.push(2);
  pque1.push(3);
  pque1.push(4);
  int x = pque1.top();
  pque1.pop();
  pque1.push(x+1);
  cout << pque1.top() << endl;
  cout << pque1.top() << endl;

  return 0;
}

6.set

set集合,在代码中具有去重的作用

#include 
#include 
using namespace std;
int main()
{
  set st;
  st.insert(1);
  st.insert(2);
  st.insert(0);
  st.erase(2);
  cout< st;
	//st.insert(1);
	//st.insert(2);
	//st.insert(3);
	//st.insert(4);
	///*if (st.count(1))
	//{
	//	cout << "yes" << endl;
	//}*/

set不可以用下标+[]的方式来索引,只能用迭代器
for (set::iterator it = st.begin(); it != st.end(); it++)
{
   cout << *it<<" ";
}
  cout << endl;
  return 0;
}

7.map

map是数据结构中的映射

#include 
#include 
using namespace std;
int main()
{
  mapmp;
  //mapmp;
  //mapmp;
  mp[2]=1;
  mp[1]=1;
  mp[3]=1;
  mp.erase(2);
  mp[2]=2;//可修改
  cout<::iterator it=mp.begin(),it!=mp.end();it++)
{
   cout<first<<" "<second<mp;
  vectorword;
  word.push_back("asd");
  word.push_back("asd");
  word.push_back("asd");
  word.push_back("asd");
  word.push_back("qwe");
  word.push_back("qwe");
  word.push_back("qwe");
  word.push_back("qwe");
  word.push_back("qwe");
  word.push_back("qwe");
  for(int i=0;i::iterator it=mp.begin();it!=mp.end();it++)
{
   cout<first<<" "<second<

8.pair

pair的实现是一个结构体,主要的两个成员变量是first second 因为是使用struct不是class,所以可以直接使用pair的成员变量。其标准库类型--pair类型定义在#include 头文件中

#include 
#include 
using namespace std;
int main()
{
  pair p={1,2};//c++11之后才支持的
  pair p1=make_pair(1,2);//老式的构造方式
  pairp2 = { 'a',2 };
//上面的定义方法和结构体类似
	struct ST
	{
		char a;
		int b;
	};
//cout << p.first << " " << p.second << endl;
	if (p1 == p)
	{
		cout << "yes" << endl;
	}
  return 0;
}

总结

较为熟练的使用STL是算法竞赛的核型,以上的代码我们并没有去深究它的底层源码,只是较为表面的使用,但在算法竞赛中应该是够用的,毕竟我们没时间去手撕它们的源码。

你可能感兴趣的:(stl,算法,c++,开发语言)