C++STL commonly used functions

  The STL of C++ provides us with many convenient operations, which can be liberated from some tedious operations in the C language. This article briefly summarizes the commonly used functions in STL and their respective commonly used functions. The content involves: stack, queue, vector, string, unordered_set, unordered_map, set, map.

stack

1. Brief description: The function of stack is the stack, that is, the queue is last in first out
. 2. Header file: #include< stack>
3. Definition format: stack<variable type> Stack name; // For example: stack< int> a ;
4. Commonly used functions:

push()  // 将变量压入栈顶
pop()  // 栈顶出栈,无返回值
top()  // 返回栈顶元素,不出栈
empty() // 判断栈是否为空,不空返回false,空返回true
size()  // 返回栈中元素的个数
swap(s,s1) // 交换两个栈(s,s1)中全部元素

queue

1. Brief description: the function completed by queue is queue, that is, first in first out
2. Header file: #include<queue>
3. Definition format: queue<variable type> queue name; // For example: queue< int> a;
4 .Common functions:

front() // 获取队首元素,但并不删除
back() // 获取队尾元素,但并不删除
pop() // 删除队首元素,无返回值
push() // 向队列中加入元素,放置队列末尾
empty() // 判断队列是否为空,不空返回false,空返回true
size() // 返回队列中元素的个数

vector

1. Brief description: vector is a container, which can be understood as an array/linked list in C++. Access can be done through []
2. Header file: #include< vector>
3. Definition format: vector<variable type> array name; / / For example: vector< int> a;
4. Commonly used functions:

push_back() // 尾部插入数组元素
pop_back() // 删除尾部数组元素
insert(a.begin()+i, num) // 将num插入第i+1个位置上,后面依次向后串位
erase(a.begin()+i) // 将第i+1个元素删除,后面依次向前串位
erase(a.begin()+i, a.begin()+j); // 删除区间[i, j-1]。区间从0开始
size() // 返回数组中元素个数
clear() // 清空数组元素,元素个数归为0
empty() // 判断数组是否为空,不空返回false,空返回true
resize() // 重新指定数组长度

begin()和end()怎么使用?
/*begin()和end()产生指向容器的第一个元素和最后一个元素的下一个位置的迭代器,常用于标记容器中元素的迭代范围。另外,容器还定义了rbegin()和rend(),返回指向容器最后一个元素和第一个元素的前一个位置的反向(reverse)迭代器。Begin()和end()与索引下标不同,所以不是int类型。*/
//输出指定的整型顺序容器的元素
Template<class T>
void printContainer(const char *msg, const T& s) {
    
    
	cout<<msg<<:;
	copy(s.begin(), s.end(), ostream_iterator<int>(cout, “ “))// ostream_iterator是用于输出的迭代器类,ostream_iterator<int>(cout, “ “) 绑定标准输出装置。“ ”代表每个字符以空格间隔
	cout<<endl;
}

string

1. Brief description: string is a string template class, which can understand character arrays. Access to elements can be completed by [], object assignment can be used =
2. Header file: #include< string>
3. Definition format: string a;
4. Common functions:

assign(s1, b, c) // 使用s1对象对a赋值,从b开始到c结束(0开始,左闭右闭)。缺省bc全部赋值
length()/size() // 返回字符串的长度
append(s1, b, c) //添加至字符串末尾,从b开始到c结束(0开始,左闭右闭)。缺省bc全部赋值
<<===!=>=> 对象间比较大小。
compare(b, c, s1,m,n) // 比较两string对象大小,bc是自己索引,mn是s1索引。均可缺省。返回值三种:大于0,等于0,小于0
swap(s2) // 本对象与s2对象交换内容
substr(m, n) // 取子串,从m索引开始,长度为n。n可缺省
replace(b,c,s1,m,n) //字符串替换,用s1的[m,n]替换原对象的[b,c]。c-b=n-m。
erase(b, c) // 删除子串[b,c],后面依次向前补。c可缺省,删除到底
insert(b, s1) // 在索引b处插入对象s1,原对象依次向后串
find(b, c) // 返回值是int型,返回出现指定字符的下标。b是内容,c是开始位置(可缺省)。常通过for循环使用,终止条件是:(position=s.find(b,c))!=string::npos

unordered_set

1. Brief description: unordered_set is equivalent to a set, and the organization of the set is disordered. The number is unique, just to judge whether it exists.
2. Header file: #include<unordered_set>
3. Definition format: unordered_set<data type> variable name; //For example: unordered_set< int> a;
4. Common functions:

find() // 查找目标数值,找到返回迭代器。常使用的模板是s,find(a) != s.end()
count() // 返回指定值出现的次数,返回值为int型
empty() // 判断是否为空,不空返回false, 空返回true
size() // 返回数组大小
insert() // 插入元素,参数即插入的元素值
emplace() // 插入元素
erase() // 删除操作,两种方式:指定数值或者是迭代器。删除指定数值,返回值是0/1,代表是否删除成功。删除迭代器,返回值是下一个位置的迭代器。失败返回s.end()。

  Note: The function of Insert() is to insert one or more elements at the specified position of the vector container; Emplace() is a new member function of C++11, which is used to insert a new element before the specified position of the vector container . There is no big difference in function between the two. The difference lies in the implementation principle: the biggest role of Emplace() is to avoid unnecessary temporary variables, and to construct elements directly at the specified position (in place), and only need to call the constructor of the class without calling the copy constructor. Its realization mainly relies on two new features of C++11: variable parameter template and perfect forwarding. "Variable parameter template" allows Emplace() to accept any parameters, and it can be applied to the construction of any object. "Perfect Forwarding" enables the received parameters to be passed to the object's constructor as is.
For more detailed content, please refer to: https://www.cnblogs.com/narjaja/p/10039509.html

unordered_map

1. Brief description: unordered_map is used for the unordered and unique arrangement of keys and values, and is used for problems with two-dimensional requirements. It is also equivalent to a one-dimensional array, the key value is the index, the value is the array value (you can use [] when assigning values, and not using [] when traversing), which can be understood as having multiple identical unordered elements in the set.
2. Header file: #include<unordered_map>
3. Definition format: unordered_map<data type 1, data type 2> Variable name // For example: unordered_map<int, int> a;
4. Common functions:

begin()/end() // 迭代器,不是int型
size() // 返回有效元素的个数
empty() // 返回数组是否为空,不空返回false,空返回true
insert() // 插入元素,成对儿插入。插入的方式有两种:{0,1},pair<int, int>(0,1)。后者更常用
erase() // 删除元素,删除有两种类型:迭代器删除或键值删除。键值删除,返回值是删除的个数。例如:num = s.erase(1)——删除键值为1的元素,并返回个数给num。迭代器删除,返回值是下一个位置的迭代器。例如:iter = s.erase(s.begin())。Iter指向第二个元素的迭代器
clear() // 清空所有元素
find() //查找给定主键的位置,返回的是迭代器。常用的判断语句时:s.find(0) != s,end()
count() //返回值为int型,返回目标主键的个数。没找到就是0了

map

1. Brief description: map is used for key value and numerical value correspondence. Sort by key value (from small to large). Equivalent to a one-dimensional array, the key value is the index, the value is the array value (you can use [] when assigning values, and not using [] when traversing), which can be understood as multiple identical ordered elements in the collection
2. Header file: # include< map>
3. Definition format: map<data type 1, data type 2> variable name // For example: map<int, int> a;
4. Common functions:

insert() // 插入元素,成对儿插入。插入的方式有两种:{0,1},pair<int, int>(0,1)。后者更常用
erase() // 删除元素,删除有两种类型:迭代器删除或键值删除。键值删除,返回值是删除的个数。例如:num = s.erase(1)——删除键值为1的元素,并返回个数给num。迭代器删除,返回值是下一个位置的迭代器。例如:iter = s.erase(s.begin())。Iter指向第二个元素的迭代器
find() //查找给定主键的位置,返回的是迭代器。常用的判断语句时:s.find(0) != s,end()
clear() // 清空所有元素
count() //返回值为int型,返回目标主键的个数。没找到就是0了
empty() // 返回数组是否为空,不空返回false,空返回true
size() // 返回有效元素的个数
iter->first/iter->second // iter代表迭代器,first访问键值,second访问数值

  For map/set, unorderedmap/unorderedset iterator is defined as follows: container class name::iterator name;
for example:
  unordered_set< int>::iterator a;
  set< int>::iterator a;
  unordered_map<int, string >::iterator a;
  map<int, string>::iterator a;

Some commonly used general algorithms in STL:

sort(a.begin(), a.end(), cmp)
header file: #include<algorithm>
Description: The function is sorting, the first two parameters are iterators, which specify the range of sorting. The third is the sorting method. By default, it will arrive from a young age by default. If customized, the return value type is staic bool cmp(int a, int b) {}

reverse(a.begin(), a,end())
Header file: #include<algorithm>
Description: The function is reverse, and the parameters are two iterators. Specify the flip range

Due to the limited level of the author of this article, if there is any error, please correct me in the comment section below, thank you!

Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/114542691