当前位置:网站首页>STL quick reference manual
STL quick reference manual
2022-07-02 08:09:00 【Villanelle#】
STL- Eight containers and their common methods of sorting
1 string Containers
notes : The function prototypes listed here const string &s Can be replaced by const char *s.
1.1 string Constructors
string();// Create an empty string for example : string str;string(const char* s);// Use string s initializationstring(const string& str);// Copy structure
1.2 string Assignment operation
string& operator=(const string &s);// Put the string s Assign to the current string
1.3 string String splicing
string& operator+=(const string& str);// heavy load += The operatorstring& append(const string &s);// Same as operator+=(const string& str)
1.4 string Find and replace
find and rfind Method not found returns -1.
int find(const string& str, int pos = 0) const;// lookup str For the first time , from pos Start looking forint rfind(const string& str, int pos = npos) const;// lookup str The last position , from pos Start looking forstring& replace(int pos, int n, const string& str);// Replacement from pos Start n Characters are strings str
1.5 string String comparison
Comparison mode : By character ASCII Compare the two codes , It is mainly used to compare whether two strings are the same , Same returns 0.
int compare(const string &s) const;// And string s Compareint compare(const char *s) const;// And string s Compare
1.6 string Character access
char& operator[](int n);// adopt [] The way to get characterschar& at(int n);// adopt at Method to get the character
1.7 string Insert and delete
string& insert(int pos, const string& str);// Insert stringstring& erase(int pos, int n = npos);// Remove from Pos At the beginning n Characters
1.8 string Substring
string substr(int pos = 0, int n = npos) const;// Return from pos At the beginning n A string of characters
2 vector Containers ( Dynamically extended array )
2.1 vector Constructors
vector<T> v;// Use template to implement class , Default constructorvector(n, elem);// The constructor will n individual elem Copy to yourself .vector(const vector &vec);// copy constructor .
2.2 vector Assignment and exchange
vector& operator=(const vector &vec);// Overload the equal sign operatorswap(vec);// take vec Swapping with its own elements , Can be used to shrink memoryvector<int>(v).swap(v); // Anonymous object
2.3 vector Capacity and size
empty();// Judge whether the container is emptycapacity();// Capacity of containersize();// Returns the number of elements in the containerresize(int num);// Reassign the length of the container to num, If the container becomes longer , Then fill in the new location with the default value . // If the container gets shorter , Then the element beyond the length of the container at the end is deleted .
resize(int num, elem);// Reassign the length of the container to num, If the container becomes longer , with elem Value to fill in the new location . // If the container gets shorter , Then the element beyond the length of the container at the end is deleted
2.4 vector Insert and delete
push_back(ele);// Tail-insert element elepop_back();// Delete the last elementinsert(const_iterator pos, ele);// The iterator points to position pos Insert elements eleerase(const_iterator pos);// Delete the element that the iterator points toclear();// Delete all elements in the container
2.5 vector Data access
at(int idx);// Returns the index idx The dataoperator[];// Returns the index idx The datafront();// Returns the first data element in the containerback();// Returns the last data element in the container
2.6 vector Reserve space
reserve(int len);// Container reservation len Element length , Reserved position is not initialized , The element is not accessible , When the amount of data is large, it can be used to reduce vector The number of expansions when dynamically expanding capacity .
3 deque Containers ( Double ended array )
3.1 deque Constructors ( Same as vector)
deque<T> deq; // Default construction formdeque(n, elem);// The constructor will n individual elem Copy to yourself .deque(const deque &deq);// copy constructor
3.2 deque Assignment and exchange ( Same as vector)
deque& operator=(const deque &deq);// Overload the equal sign operatorswap(deq);// take deq Swapping with its own elements
3.3 deque Size operation ( And vector Compared to none capacity)
deque.empty();// Judge whether the container is emptydeque.size();// Returns the number of elements in the containerdeque.resize(num);// Reassign the length of the container to num, If the container becomes longer , Then fill in the new location with the default value . // If the container gets shorter , Then the element beyond the length of the container at the end is deleted .
deque.resize(num, elem);// Reassign the length of the container to num, If the container becomes longer , with elem Value to fill in the new location . // If the container gets shorter , Then the element beyond the length of the container at the end is deleted .
3.4 deque Insert and delete ( Than vector More head operation )
push_back(elem);// Add a data at the end of the containerpush_front(elem);// Insert a data in the head of the containerpop_back();// Delete the last data in the containerpop_front();// Delete the first data of the containerinsert(pos,elem);// stay pos Insert a elem Copies of elements , Return bits of new dataclear();// Empty all the data in the containererase(pos);// Delete pos Location data , Return to the next data location
3.5 deque Data access ( Same as vector)
at(int idx);// Returns the index idx The dataoperator[];// Returns the index idx The datafront();// Returns the first data element in the containerback();// Returns the last data element in the container
4 stack Containers ( Stack )
4.1 stack Constructors
stack<T> stk;//stack Using template class to achieve , stack Object's default construction formstack(const stack &stk);// copy constructor
4.2 stack Assignment and exchange
stack& operator=(const stack &stk);// Overload the equal sign operatorswap(stk);// take stk Swapping with its own elements
4.3 stack Size operation
empty();// Determine if the stack is emptysize();// Return the stack size
4.4 stack Insert and delete
push(elem);// Add elements to the top of the stackpop();// Remove the first element from the top of the stack
4.5 stack Data access
top();// Back to top of stack element
5 queue Containers ( queue )
5.1 queue Constructors
queue<T> que;//queue Using template class to achieve ,queue Object's default construction formqueue(const queue &que);// copy constructor
5.2 queue Assignment and exchange
queue& operator=(const queue &que);// Overload the equal sign operatorswap(que);// take que Swapping with its own elements
5.3 queue Size operation
empty();// Determine if the stack is emptysize();// Return the stack size
5.4 queue Insert and delete
push(elem);// Add elements to the end of the teampop();// Remove the first element from the team leader
5.5 queue Data access
back();// Return to the last elementfront();// Returns the first element
6 list Containers ( Bidirectional circular linked list )
List There is an important property , Neither the insert operation nor the delete operation will cause the original list The failure of iterators , This is in vector It's not true .
6.1 list Constructors
list<T> lst;//list Use template class to implement , Object's default construction form :list(n,elem);// The constructor will n individual elem Copy to yourself .list(const list &lst);// copy constructor .
6.2 list Assignment and exchange
list& operator=(const list &lst);// Overload the equal sign operatorswap(lst);// take lst Swapping with its own elements .
6.3 list Size operation
size();// Returns the number of elements in the containerempty();// Judge whether the container is emptyresize(num);// Reassign the length of the container to num, If the container becomes longer , Then fill in the new location with the default value . // If the container gets shorter , Then the element beyond the length of the container at the end is deleted .
resize(num, elem);// Reassign the length of the container to num, If the container becomes longer , with elem Value to fill in the new location .// If the container gets shorter , Then the element beyond the length of the container at the end is deleted .
6.4 list Insert deletion
push_back(elem);// Add an element at the end of the containerpop_back();// Delete the last element in the containerpush_front(elem);// Insert an element at the beginning of the containerpop_front();// Remove the first element from the beginning of the containerinsert(pos,elem);// stay pos Position plug in elem Copies of elements , Returns the location of the new data .clear();// Remove all data from the containererase(pos);// Delete pos Location data , Return to the next data location .remove(elem);// Delete all and in the container elem Value matching elements .
6.5 list Data access
front();// Returns the first element .back();// Return to the last element .
6.6 list Reverse and sort ( Member functions )
reverse();// Reverse a linked listsort();// List sortingsort(MyCompare);// Use functor to customize sorting rules
class MyCompare
{
public:
bool operator()(int v1, int v2) {
return v1 > v2; // Ascending
}
};
sort(ComparePerson);// if list Store custom data types , Sorting rules must be specified when sorting
bool ComparePerson(Person& p1, Person& p2) {
if (p1.m_Age == p2.m_Age) {
return p1.m_Height > p2.m_Height; // If the age is the same, in descending order of height
}
else{
return p1.m_Age < p2.m_Age; // Ascending by age
}
}
7 set/multiset Containers ( aggregate / Allow duplicate sets )
7.1 set Constructors
set<T> st;// Default constructor :set(const set &st);// copy constructor
7.2 set Assignment and exchange
set& operator=(const set &st);// Overload the equal sign operatorswap(st);// Swap two collection containers
7.3 set Size operation
size();// Returns the number of elements in the containerempty();// Judge whether the container is empty
7.4 set Insert and delete
insert(elem);// Insert elements into the container .clear();// Remove all elementserase(pos);// Delete pos The element that iterators refer to , Returns the iterator of the next element .erase(elem);// Delete the value in the container as elem The elements of .
7.5 set Find and count
find(key);// lookup key Whether there is , If exist , Returns the iterator of the element of the key ; If it does not exist , return set.end();count(key);// Statistics key Number of elements of ( about set The result is 0 or 1)
7.6 set and multiset difference
- set You can't insert duplicate data , and multiset Sure
- set When the data is inserted, the insertion result will be returned , Indicates whether the insertion was successful
- multiset It doesn't detect data , So you can insert duplicate data
7.7 set Container sorting
Automatically sort when inserting elements , Default is from small to large , You can use functors to customize sorting rules when defining containers .
set<int, MyCompare> s;//Mycompare It's a custom class , Overloaded in class ()( The predicate )
class MyCompare
{
public:
bool operator()(int v1, int v2) {
return v1 > v2; // Ascending
}
};
set<Person, comparePerson> s;//set Store custom data types , Collation must be specified
bool ComparePerson(Person& p1, Person& p2) {
if (p1.m_Age == p2.m_Age) {
return p1.m_Height > p2.m_Height; // If the age is the same, in descending order of height
}
else{
return p1.m_Age < p2.m_Age; // Ascending by age
}
}
8 map/multimap Containers ( Key value pair set )
8.1 pair For group constructors
pair<type, type> p ( value1, value2 );pair<type, type> p = make_pair( value1, value2 );
8.2 map Constructors
map<T1, T2> mp;//map Default constructor :map(const map &mp);// copy constructor
8.3 map Assignment and exchange
map& operator=(const map &mp);// Overload the equal sign operatorswap(st);// Swap two collection containers
8.4 map Size operation
size();// Returns the number of elements in the containerempty();// Judge whether the container is empty
8.5 map Insert and delete
insert(elem);// Insert elements into the container .clear();// Remove all elementserase(pos);// Delete pos The element that iterators refer to , Returns the iterator of the next element .erase(key);// Delete the value in the container as key The elements of .
8.6 map Find and count
find(key);// lookup key Whether there is , If exist , Returns the iterator of the element of the key ; If it does not exist , return set.end();count(key);// Statistics key Number of elements of ( about map The result is 0 or 1)
8.7 map Container sorting
Automatically sort when inserting key value pairs , By default key From small to large , You can use functors to customize sorting rules when defining containers .
map<int, int, MyCompare> m;// Here MyCompare For custom collation , Same as set Containersmap<Person, int, comparePerson> s;// If the key value is a user-defined data type ( To use less ), Collation must be specified , Same as set Containers
边栏推荐
- Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
- 力扣每日一题刷题总结:链表篇(持续更新)
- 浅谈深度学习中的对抗样本及其生成方法
- Target detection for long tail distribution -- balanced group softmax
- 使用C#语言来进行json串的接收
- Income in the first month of naked resignation
- OpenCV3 6.2 低通滤波器的使用
- 【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
- 【学习笔记】反向误差传播之数值微分
- C语言实现XML生成解析库(XML扩展)
猜你喜欢

利用Transformer来进行目标检测和语义分割

Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?

VS Code配置问题

How to wrap qstring strings

Eklavya -- infer the parameters of functions in binary files using neural network
![[binocular vision] binocular correction](/img/fe/27fda48c36ca529eec21c631737526.png)
[binocular vision] binocular correction

Carsim problem failed to start Solver: Path Id Obj (X) was set to y; Aucune valeur de correction de xxxxx?

Sequence problem for tqdm and print

C language implements XML generation and parsing library (XML extension)

【MagNet】《Progressive Semantic Segmentation》
随机推荐
AR system summary harvest
SQLyog远程连接centos7系统下的MySQL数据库
多站点高可用部署
SQL操作数据库语法
Hystrix dashboard cannot find hystrix Stream solution
简易打包工具的安装与使用
力扣方法总结:查找类
针对语义分割的真实世界的对抗样本攻击
Remplacer l'auto - attention par MLP
力扣每日一题刷题总结:链表篇(持续更新)
Embedding malware into neural networks
Command line is too long
Rhel7 operation level introduction and switching operation
In the era of short video, how to ensure that works are more popular?
Meta Learning 简述
OpenCV常用方法出处链接(持续更新)
[learning notes] matlab self compiled Gaussian smoother +sobel operator derivation
关于原型图的深入理解
Global and Chinese markets for conventional rubber track 2022-2028: Research Report on technology, participants, trends, market size and share
常量指针和指针常量