Summary of common C++STL

Posted by MrAdam on Thu, 07 Oct 2021 20:56:45 +0200

Summary of common C++STL

1, vector, variable length array, multiplication idea

size() returns the number of elements
empty() returns whether it is empty
front()/back() returns the head or tail of the array
push_back() insert element at tail
pop_ Delete element at the end of back()
Iterator: begin()/end()
Support random addressing: []

Support comparison operation

Vectors are compared in dictionary order

	vector<int>a(3,4);
	vector<int>b(4,3);
	if(a > b) puts("Yes");//Yes
	else puts("No");

Three traversal modes

	vector<int>v;
	for(int i=0;i<v.size();i++)
	{
		cout<<a[i]<<' ';
	}
	cout<<endl;
	for(vector<int>::iterator i = v.begin();i!=v.end();i++)//auto i = v.begin()
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	for(auto x:v)
	{
		cout<<x<<" ";
	}
	cout<<endl;

2, Pair <, > binary template

pair<string,int>
First, the first element
Second, the second element
Support comparison operation, with first as the first keyword and second as the second keyword (dictionary order)

Initialization mode

	pair<string,int>p;
	p = make_pair("yxc",18);
	p = {"yxc",18};
	pair<string,pair<string,int>>pp1;

3, String string

size()/length()
empty()
clear()

Support self adding operation

	string s = "yxc";
	s+='n';
	s+="nb";
	cout<<s<<endl;//yxcnnb

substr() substring

Substr (start position, substring length): returns a fixed length character starting at the start position
Substr (start position): returns all characters from the start position

4, Queue queue

push()
front()
back(): returns the end of queue element
pop()
size()
empty()
There is no clear() function, so it needs to be reconstructed when emptying

5, priority_queue priority queue (heap)

priority_queue, the default is the large root heap
push() inserts an element
top() returns the heap top element
pop() pops the top of heap element

Define small root heap

Method 1

	priority_queue<int,vector<int>,greater<int>>heap; 

Method 2: insert opposite number

	priority_queue<int>heap;
	heap.push(-x);

6, Stack stack

push()
pop()
top()
empty()
size()
There is no clear() function, so it needs to be reconstructed when emptying

7, deque Dual Ended queue

size()
empty()
clear()
front()
back()
push_back()
pop_back()
push_front()
pop_back()
Iterator: begin()/end()
Support random addressing: []
In general, deque is not used because the efficiency is very low

8, set /multiset set

There are no duplicate elements in set, which will be automatically ignored when inserting duplicate elements
Duplicate elements are supported in multiset

insert(): inserts a number
size()
empty()
clear()
find(): find a number and return the end iterator if it does not exist
count(): returns the number of occurrences of a number
erase()
(1) Enter a number x and delete all x O (k + logn); k is the number of X
(2) Enter an iterator and delete it
lower_bound(x): returns the smallest number greater than or equal to X
upper_bound(x): returns the smallest number greater than x

9, map/multimap key value pair container

insert() inserts a pair
The input parameter of erase() is pair or iterator
find()
size()
empty()
clear()
begin()/end() + + -- returns the predecessor and successor
[] the time complexity is O(logn)
lower_bound(x): returns the smallest number greater than or equal to X
upper_bound(x): returns the smallest number greater than x

unordered_set,unordered_map,unordered_multiset,unordered_multimap, hash table
Similar to the above, the time complexity of adding, deleting, modifying and querying is O(1)
lower_bound()/upper_bound() is not supported

#include <map>
#include <string>
using namespace std;
...
map<string, string> namemap;
//Increase...
namemap["yue buqun"]="The leader of Huashan sect is called Gentleman sword";
namemap["Zhang Sanfeng"]="Leader of Wudang, founder of Taijiquan";
namemap["invincible eastern"]="The first master, sunflower classic";
...
//Find..
if(namemap.find("yue buqun") != namemap.end()){
        cout<<"Find!<<endl;
}

10, bitset pressure

For example, if you want to compress 1024 bool variables (1024 bytes) into 1024 1bit variables (256 bytes), you can save a lot of space

method

bitset<10000> s;
~:Reverse
&:And
|: or
^: XOR
>>: Shift right
<<: Shift left
==: Yes 0 is 1
!=: Yes 0 is 1
[ ]: Take out a bit
count():Returns how many 1s there are
any():Returns whether there is at least one 1
none():Returns whether all are 0
set():Put all positions 1
reset():Set all positions to 0
set(k,v):Will be the first k Bit becomes v
flip() Equivalent to negation
flip(k):The first k Bit inversion

Topics: C++ data structure Cpp