Overview of common methods of C++ STL

algorithm

C++ STL (Standard Template Library) provides many algorithmic methods, which are encapsulated in header files. These algorithms can be applied to various containers such as vector, deque, list, set, map, etc.

Here are some commonly used algorithmic methods and their examples:

1. find(): Find whether the specified element exists in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
    
    
    cout << "Found element: " << *it << endl;
} else {
    
    
    cout << "Element not found" << endl;
}

2. sort(): Sort the elements in the container.

Example:

vector<int> vec = {
    
    3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(vec.begin(), vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

3. reverse(): Flip the elements in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
reverse(vec.begin(), vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

4. accumulate(): Calculate the sum of the elements in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
int sum = accumulate(vec.begin(), vec.end(), 0);
cout << "Sum of elements: " << sum << endl;

5. count(): Calculate the number of specified elements in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5, 3, 2, 1};
int cnt = count(vec.begin(), vec.end(), 3);
cout << "Number of 3s: " << cnt << endl;

6. max_element(): Returns the largest element in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = max_element(vec.begin(), vec.end());
cout << "Max element: " << *it << endl;

7. min_element(): Returns the smallest element in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = min_element(vec.begin(), vec.end());
cout << "Min element: " << *it << endl;

8. find_if(): Find elements in the container that meet the specified conditions.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = find_if(vec.begin(), vec.end(), [](int n) {
    
     return n % 2 == 0; });
if (it != vec.end()) {
    
    
    cout << "Found even element: " << *it << endl;
} else {
    
    
    cout << "Even element not found" << endl;
}

9. remove(): Delete the specified element from the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = remove(vec.begin(), vec.end(), 3);
vec.erase(it, vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

10. transform(): Transform the elements in the container.

Example:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
vector<int> vec2;
transform(vec.begin(), vec.end(), back_inserter(vec2), [](int n) {
    
     return n * 2; });
for (auto i : vec2) {
    
    
    cout << i << " ";
}

container

1. vector

Vector is a dynamic array container that provides the following common methods:

- push_back() function: insert elements at the end of the array

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
// v中包含3个元素,分别是1, 2, 3

- pop_back() function: Pop the element at the end of the array

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.pop_back();
// v中包含2个元素,分别是1, 2

- insert() function: insert elements

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
auto it = v.begin();
it++; // it指向第二个元素
v.insert(it, 4);
// v中包含4个元素,分别是1, 4, 2, 3

- erase() function: delete elements

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
auto it = v.begin();
it++; // it指向第二个元素
v.erase(it);
// v中包含2个元素,分别是1, 3

- empty() function: determine whether the vector is empty

vector<int> v;
bool isEmpty = v.empty(); // isEmpty的值为true

- resize() function: change the size of the array

vector<int> v;
v.resize(3);
// v中包含3个元素,都是0
v.resize(5, 1);
// v中包含5个元素,前3个是0,后2个是1

2. therefore

Deque is a double-ended queue container that provides the following common methods:

- push_back() function: insert an element at the end of the queue

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
// d中包含3个元素,分别是1, 2, 3

- push_front() function: insert elements at the head of the queue

deque<int> d;
d.push_front(1);
d.push_front(2);
d.push_front(3);
// d中包含3个元素,分别是3, 2, 1

- pop_back() function: Pop the element at the end of the queue

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_back();
// d中包含2个元素,分别是1, 2

- pop_front() function: Pop the head element of the queue

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_front();
// d中包含2个元素,分别是2, 3

- insert() function: insert elements

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
auto it = d.begin();
it++; // it指向第二个元素
d.insert(it, 4);
// d中包含4个元素,分别是1, 4, 2, 3

- erase() function: delete elements

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
auto it = d.begin();
it++; // it指向第二个元素
d.erase(it);
// d中包含2个元素,分别是1, 3

- empty() function: determine whether the deque is empty

deque<int> d;
bool isEmpty = d.empty(); // isEmpty的值为true

- resize() function: change the queue size

deque<int> d;
d.resize(3);
// d中包含3个元素,都是0
d.resize(5, 1);
// d中包含5个元素,前3个是0,后2个是1

3. set

set is a collection container that provides the following common methods:

- insert() function: insert elements

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
// s中包含3个元素,分别是1, 2, 3

- erase() function: delete elements

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.erase(2);
// s中包含2个元素,分别是1, 3

- find() function: find elements

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
auto it = s.find(2);
if (it != s.end()) {
    
    
    cout << "2 is in the set" << endl;
}

- empty() function: determine whether the set is empty

set<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true

- Custom collation

If you need to sort the elements in the set according to a custom sorting rule, you can use a custom comparison function or a custom comparison class. For example, if you want to arrange elements in ascending order, you can define a less than operator:

struct cmp {
    
    
    bool operator()(const int& a, const int& b) {
    
    
        return a < b; // 从小到大排序
    }
};
set<int, cmp> s;
s.insert(3);
s.insert(2);
s.insert(1);
// s中包含3个元素,分别是1, 2, 3,按照从小到大排序

4. list

list is a doubly linked list container that provides the following common methods:

- push_back() function: insert elements at the end of the linked list

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
// l中包含3个元素,分别是1, 2, 3

- push_front() function: insert elements at the head of the linked list

list<int> l;
l.push_front(1);
l.push_front(2);
l.push_front(3);
// l中包含3个元素,分别是3, 2, 1

- pop_back() function: Pop the element at the end of the linked list

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_back();
// l中包含2个元素,分别是1, 2

- pop_front() function: Pop up the head element of the linked list

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_front();
// l中包含2个元素,分别是2, 3

- erase() function: delete elements

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
auto it = l.begin();
it++; // it指向第二个元素
l.erase(it);
// l中包含2个元素,分别是1, 3

- empty() function: determine whether the list is empty

list<int> l;
bool isEmpty = l.empty(); // isEmpty的值为true

- Custom collation

list does not support custom sorting rules, because it is a linked list rather than an ordered container. If you need to sort elements according to a custom sorting rule, you can consider using containers such as set, vector, and deque.
Stack and map in C++ STL are two commonly used containers for implementing stacks and maps. Here is an overview of their methods with examples:

5. stack

stack is a last-in-first-out (LIFO) container that provides the following common methods:

- push() function: insert an element at the top of the stack

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
// 栈中元素为3,2,1

- pop() function: Pop the top element of the stack

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
// 栈中元素为2,1

- top() function: access the top element of the stack

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
int x = s.top(); // x的值为3

- empty() function: determine whether the stack is empty

stack<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true

6. map

map is a key-value pair mapping container that provides the following common methods:

- insert() function: insert key-value pairs

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
// m中包含3个键值对,分别是{"Alice": 20}, {"Bob": 25}, {"Charlie": 30}

- erase() function: delete key-value pairs

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
m.erase("Bob");
// m中包含2个键值对,分别是{"Alice": 20}, {"Charlie": 30}

- find() function: find the value corresponding to the key

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
auto it = m.find("Bob");
if (it != m.end()) {
    
    
    int age = it->second; // age的值为25
}

- empty() function: determine whether the map is empty

map<string, int> m;
bool isEmpty = m.empty(); // isEmpty的值为true

- Custom collation

If you need to sort the keys in the map according to a custom sorting rule, you can use a custom comparison function or a custom comparison class. For example, if you want to order the keys from smallest to largest, you can define a less than operator:

struct cmp {
    
    
    bool operator()(const string& a, const string& b) {
    
    
        return a < b; // 从小到大排序
    }
};
map<string, int, cmp> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
// m中包含3个键值对,分别是{"Alice": 20}, {"Bob": 25}, {"Charlie": 30},按照键从小到大排序

7. queue

queue is a first-in-first-out (FIFO) container that provides the following common methods:

- push() function: insert elements at the end of the queue

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
// 队列中元素为1,2,3

- pop() function: Pop up the first element of the queue

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.pop();
// 队列中元素为2,3

- front() function: access the first element of the queue

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.front(); // x的值为1

- back() function: access the tail element of the queue

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.back(); // x的值为3

- empty() function: determine whether the queue is empty

queue<int> q;
bool isEmpty = q.empty(); // isEmpty的值为true

8. priority_queue

priority_queue is a priority queue, and each pop-up element is the element with the highest priority in the queue. By default, elements are prioritized in order from largest to smallest. priority_queue provides the following common methods:

- push() function: insert elements at the end of the queue

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
// 队列中元素为3,2,1

- pop() function: Pop up the first element of the queue

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
pq.pop();
// 队列中元素为2,1

- top() function: access the first element of the queue

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
int x = pq.top(); // x的值为3

- empty() function: determine whether the queue is empty

priority_queue<int> pq;
bool isEmpty = pq.empty(); // isEmpty的值为true

- Custom collation

If you need to customize the collation of elements, you can achieve it by overloading the operator. For example, if you want to arrange elements in ascending order, you can define a less than operator:

struct cmp {
    
    
    bool operator()(int a, int b) {
    
    
        return a > b; // 从小到大排序
    }
};
priority_queue<int, vector<int>, cmp> pq;
pq.push(3);
pq.push(1);
pq.push(2);
// 队列中元素为1,2,3

String

string in C++ STL is a class that represents a variable-length string. The string class provides many function methods for manipulating and processing strings. The following are some commonly used string function methods and examples of their usage:

1. Constructor

- default constructor

string s1; // s1是一个空字符串

- Constructor with parameters

string s2("hello"); // s2是一个字符串,内容为"hello"
string s3(5, 'a'); // s3是一个字符串,内容为"aaaaa"

2. Assignment operation

- assignment operator

string s1 = "hello";
string s2 = s1; // s2的内容和s1相同
string s3;
s3 = s1; // s3的内容和s1相同

- assign() function

string s1 = "hello";
string s2;
s2.assign(s1); // s2的内容和s1相同
string s3;
s3.assign("world"); // s3的内容为"world"

3. Access operations

- [] operator

string s = "hello";
char c = s[0]; // c的值为'h'

- at() function

string s = "hello";
char c = s.at(0); // c的值为'h'

- front() function

string s = "hello";
char c = s.front(); // c的值为'h'

- back() function

string s = "hello";
char c = s.back(); // c的值为'o'

4. Insert operation

- push_back() function

string s = "hello";
s.push_back('!'); // s的内容为"hello!"

- insert() function

string s = "hello";
s.insert(0, "world"); // s的内容为"worldhello"

5. Delete operation

- pop_back() function

string s = "hello!";
s.pop_back(); // s的内容为"hello"

- erase() function

string s = "hello world";
s.erase(6, 5); // s的内容为"hello"

6. Find operation

- find() function

string s = "hello world";
int pos = s.find("world"); // pos的值为6

- rfind() function

string s = "hello world";
int pos = s.rfind("l"); // pos的值为9

7. Replace operation

- replace() function

string s = "hello world";
s.replace(6, 5, "you"); // s的内容为"hello you"

8. Substring operation

- substr() function

string s = "hello world";
string sub = s.substr(0, 5); // sub的内容为"hello"

9. Other operations

- length() function

string s = "hello world";
int len = s.length(); // len的值为11

- size() function

string s = "hello world";
int len = s.size(); // len的值为11

- empty() function

string s = "hello world";
bool isEmpty = s.empty(); // isEmpty的值为false

Guess you like

Origin blog.csdn.net/gezongbo/article/details/130184105