当前位置:网站首页>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
边栏推荐
- Correction binoculaire
- jetson nano安装tensorflow踩坑记录(scipy1.4.1)
- 力扣每日一题刷题总结:栈与队列篇(持续更新)
- Erase method in string
- Carsim-实时仿真的动画同步问题
- Hystrix dashboard cannot find hystrix Stream solution
- install.img制作方式
- OpenCV 6.4 中值滤波器的使用
- Jetson nano installation tensorflow stepping pit record (scipy1.4.1)
- 应对长尾分布的目标检测 -- Balanced Group Softmax
猜你喜欢
Specification for package drawing
静态库和动态库
【MagNet】《Progressive Semantic Segmentation》
It's great to save 10000 pictures of girls
Carsim problem failed to start Solver: Path Id Obj (X) was set to y; Aucune valeur de correction de xxxxx?
Using transformer for object detection and semantic segmentation
Carsim-实时仿真的动画同步问题
Where do you find the materials for those articles that have read 10000?
针对tqdm和print的顺序问题
On November 24, we celebrate the "full moon"
随机推荐
力扣每日一题刷题总结:二叉树篇(持续更新)
E-R画图明确内容
学习写文章格式
服务器的内网可以访问,外网却不能访问的问题
My VIM profile
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
Jupyter Notebook常用快捷键(在命令模式中按H也可查看)
Static library and dynamic library
Fundamentals of music theory (brief introduction)
Global and Chinese markets for conventional rubber track 2022-2028: Research Report on technology, participants, trends, market size and share
Using super ball embedding to enhance confrontation training
Carsim-实时仿真的动画同步问题
Organigramme des activités
C#与MySQL数据库连接
Longest isometric subsequence
【Batch】learning notes
Simply test the two different data transmission methods of content length and chunked
(15) Flick custom source
Constant pointer and pointer constant
解决jetson nano安装onnx错误(ERROR: Failed building wheel for onnx)总结