STL-- containers and algorithms Detailed (10 minutes Quick Start)

STL and algorithm header file

STL is a collection container, these containers have a list, vector, set, map, etc.

algorithm is a container inherit some arithmetic functions, auxiliary brush algorithm problem

The sort () function to sort iterator (pointer may be) as a parameter

1.string string

Operator + = 1.1

the string header file + = overloaded, an acceptable parameter, string, characters, ASCII characters

Case ASCII code, A65, a97, an interval between the 32

### 1.2sort application thereof ()

string s="123";
sort(s.begin(),s.end());

Output 123

1.3erase method

Such as deleting a character string in the first and last character

string s="123";
s.erase(s.begin());
s.erase(--s.end());

begin () is the first iterator, end () is the end iterator

1.4substr method

string s="5418340";
string s1=s.substr(1,3);//取418,取索引为1,往后截断3个
string s2=s.substr(1,-1);//索引为1,截断到最后

1.5 round-robin fashion

1.5.1for cycle

for(int i=0;i<s.length();i++)//s.length()返回字符串长度

1.5.2 using the iterator loop

string s="123";
//常规写法
for(string::iterator<string> it=s.begin();it!=s.end();it++)
    cout<<*it;

1.5.3 Starter auto iterator

string s="123";

//简便写法,采用auto
for(auto it=s.begin();it!=s.end();it++)
    cout<<*it;//取出来的是char类型

1.5.4C ++ 11 new features

string s="123";

//C++11新特性,s需要是一个可遍历的容器或流,x用来在遍历过程中获得容器的每一个元素,相当于java中的for each
for(auto x:s)
    cout<<x;

2.vector vector

2.1vector basis

vector<int> v;//定义空向量
for(auto x:v) cout<<x;//输出NULL,空

vector<int> v2(4);
for(auto x:v2) cout<<x;//输出4个0

vector<int> v3(4,6);
for(auto x:v3) cout<<x;//输出4个6

//也可以使用大括号定义
vector<int> v{1,2,3,4,5,6};

//获取元素方式
cout<<v[0];
cout<<v.at(0);//两种方式等价

//循环
for(int i=0;i<v.size();i++);
for(vector<int>::iterator it=v.begin();it<v.end();it++);
for(auto it = v.begin();it<v.end();it++);
for(auto x:v) cout<<x;

2.2 Method

v.push_back(5);//追加内容,int型5
v.resize(10);//调整大小,多出来的默认为0
v.erase(v.begin());//删除元素,复杂度为O(n)

//获取第一个元素
v.front();
v[0];
*v.begin();

//获取最后一个元素
v.back();
v[v.size()-1];//size方法获取向量大小
*(--v.end());

2.3 Sorting

vector<int> v{5,1,2,5,4,0,-1};

sort(v.begin(),v.end());//默认从小到大排序
sort(v.begin(),v.end(),less<int>())//带比较器从小到大排序

sort(v.begin(),v.end(),greater<int>());//带比较器从大到小排序

3.stack stack

3.1stack basis

#include<stack>
//构造方法,一般使用空参构造
stack<int> s;

3.2 Method

push、pop、size、empty

  • pop does not return value
  • Returns the number of stack size
s.push(2);//压栈
int num = s.top();//取栈顶元素
s.pop();
s.size();

3.3 Application

3.3.1 binary conversion

//把10进制转换为2进制
int itob(int decimal)
{
    int ans=0;
    stack<int> s;
    while(decimal!=0)
    {
        s.push(decimal%2);
        decimal/=2;
    }
    while(!s.empty())
    {
        ans=ans*10+s.top();
        s.pop();
    }
    return res;
}
//input 20 
//output 10100

3.3.2 reverse word (expand sstream, stoi, itoa)

Line input string, the string in reverse play

hello world

//方法1,采用流,sstream
#include <sstream>
int main()
{
    string str;
	getline(cin,str);
	stringstream ss;
	ss<<str;//str内容流入ss流对象中
	while(ss>>str)
	{
    	cout<<str;
	}
	return 0;
}


//方法2,采用stack
int main()
{
    string str;
    stack<string> s;
	getline(cin,str);
	stringstream ss;
	ss<<str;//str内容流入ss流对象中
	while(ss>>str)
    	s.push(str);
	
    while(!s.empty())
    {
        cout<<s.top();
        s.pop()
        //oj不允许最后一行为换行符
        if(s.size()!=1) cout<<" ";
    }
    
	return 0;
}
//输出为 world hello

3.3.3 string-to-digital

//方法1,sstream,利用流和cout对象自身对输入的转换
#include <sstream>
int main()
{
    string s="1234";
    int i;
    stringstream ss;
    ss<<s;
    ss>>i;
    cout<<i;
    return 0;
}

//方法2,string中的函数stoi()
#include <string>
int main()
{
    string s="1234";
    int i = stoi(s);
    cout<<i;
    return 0;
}

3.3.4 Digital to String

//方法1,sstream,利用流和cout对象自身对输入的转换
#include <sstream>
int main()
{
    int a=1234;
    string out;
    stringstream ss;
    ss<<a;
    ss>>out;
    cout<<out<<endl;
    return 0;
}

//方法2,c++新特性,to_string()函数

int a=1234;
cout<<to_string(a)<<endl;

//方法3,itoa()将整型转为字符串
需要#include<cstdlib>

4.queue queue

4.1 Basic operation queue

#include<queue>
q.push(5);
q.push(6);
cout<<q.front()<<endl;
q.pop();
cout<<q.front()<<endl;
cout<<q.size()<<endl;

5.map & unordered_map map

4.1map basic operations

Concept: Mapping

The bottom layer is a tree map; unordered_map underlying structure is a hash

#include<map>

map<int,int> m;//map底层为有序的树状结构
m[6]=3;
m[5]=8;
m[4]=9;
for(auto it = m.brgin();it!=m.end();it++)
  cout<<it->first<<" "<<it->second<<endl;
//map输出4 9;5 8;6 3;说明map内部是按照first元素升序排序的
for(auto tmp:m)
    cout<<tmp.first<<" "<<tmp.second<<endl;
  

4.2unordered_map

#include<unordered_map>

unordered_map<int,int> m;//底层为无序的哈希结构,快很多
m[6]=3;
m[5]=8;
m[4]=9;
for(auto it = m.brgin();it!=m.end();it++)
  cout<<it->first<<" "<<it->second<<endl;
//输出4 9;5 8;6 3;内部有时候按照first元素升序排序的,有时候无序
for(auto tmp:m)
    cout<<tmp.first<<" "<<tmp.second<<endl;
  

4.3pair usage

Map sort of operation, map elements is a pair structure, structural element pair is mapped

pair need not add any header, as it is defined in the header file utility inside.

#include<algorithm>
#include<iostream>
#include<map>
#include<unordered_map>
#include<vector>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b)
{
	return a.first < b.first;//升序排列
}

int main()
{
	unordered_map<int, int> m;//底层为无序的哈希结构,快很多
	m[6] = 3;
	m[5] = 8;
	m[4] = 9;

#if 0
	sort(m.begin(), m.end(), cmp);
	for (auto tmp : m)
		cout << tmp.first << " " << tmp.second << endl;
	//输出报错,说明其不可排序,需要把map转换为vector结构
#endif

	vector<pair<int, int>> v(m.begin(), m.end());
	//用m的迭代器初始化vector v
	sort(v.begin(),v.end(),cmp);
	for (auto tmp : v)
		cout << tmp.first << " " << tmp.second << endl;
}

6.set&unordered_set集合

6.1set basic use

set is a collection of

#include<set>
#include<iostream>
using namespace std;
int main()
{
	set<int> s;
	s.insert(3);
	s.insert(4);
	s.insert(4);
	cout << s.size() << endl;
	for (auto tmp : s)
	{
		cout << tmp << " ";
	}
	cout << endl;
	for (auto it = s.begin(); it != s.end(); it++)
		cout << *it << " ";
	cout << endl;
	return 0;
}

set is a tree structure, and orderly; unodered_set is a hash structure, disordered, fast

7.deque deque

deque deque is, i.e., the tube can be understood to be operable in the back in front of

#include<iostream>
#include<deque>
using namespace std;

int main()
{
	deque<int> d;
	d.push_back(1);
	d.push_back(2);
	cout << d.back() << endl//2
		<< d.front() << endl;//1

	d.push_front(9);
	d.push_front(4);
	//现在存的是4912
	d.pop_back();//2
	d.pop_front();//4
	for (auto tmp : d) cout << tmp << endl;
	for (auto it = d.begin(); it != d.end(); it++) cout << *it;
	return 0;
}

Sequence

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;

int main()
{
	deque<int> d;
	d.push_back(1);
	d.push_back(2);
	cout << d.back() << endl//2
		<< d.front() << endl;//1

	d.push_front(9);
	d.push_front(4);
	//现在存的是4912
	
	sort(d.begin(), d.end(), greater<int>());
    //greater<int>()从大到小排序

	for (auto tmp : d) cout << tmp <<" ";
	//for (auto it = d.begin(); it != d.end(); it++) cout << *it;
	return 0;
}

8.list doubly linked list

list is a doubly linked list

list insertion and deletion are O (1), the query is O (N),

Instructions

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> li;
    li.push_back(6);
    li.push_front(5);
    li.emplace_front(9);
    li.emplace_back(10);
    li.insert(++li.begin(),2);
    for(auto tmp:li) cout<<tmp<<endl;
    for(auto it = li.begin();it!=li.end();it++)
        cout<<*it<<endl;
    return 0;
}

9 do not know where to check documents

English documentation

Chinese documents

Published 49 original articles · won praise 29 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_41122796/article/details/104782535