C++ STL常用方法概述
算法
C++ STL(标准模板库)提供了许多算法方法,这些方法被封装在头文件中。这些算法可以应用于各种容器,如vector、deque、list、set、map等。
以下是一些常用的算法方法及其示例:
1. find():查找容器中是否存在指定元素。
示例:
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():对容器中的元素进行排序。
示例:
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():将容器中的元素翻转。
示例:
vector<int> vec = {1, 2, 3, 4, 5};
reverse(vec.begin(), vec.end());
for (auto i : vec) {
cout << i << " ";
}
4. accumulate():计算容器中的元素总和。
示例:
vector<int> vec = {1, 2, 3, 4, 5};
int sum = accumulate(vec.begin(), vec.end(), 0);
cout << "Sum of elements: " << sum << endl;
5. count():计算容器中指定元素的个数。
示例:
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():返回容器中的最大元素。
示例:
vector<int> vec = {1, 2, 3, 4, 5};
auto it = max_element(vec.begin(), vec.end());
cout << "Max element: " << *it << endl;
7. min_element():返回容器中的最小元素。
示例:
vector<int> vec = {1, 2, 3, 4, 5};
auto it = min_element(vec.begin(), vec.end());
cout << "Min element: " << *it << endl;
8. find_if():查找容器中满足指定条件的元素。
示例:
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():从容器中删除指定元素。
示例:
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():对容器中的元素进行变换。
示例:
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 << " ";
}
容器
1. vector
vector是一种动态数组容器,提供了以下常用方法:
- push_back()函数:在数组末尾插入元素
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
// v中包含3个元素,分别是1, 2, 3
- pop_back()函数:弹出数组末尾元素
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.pop_back();
// v中包含2个元素,分别是1, 2
- insert()函数:插入元素
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()函数:删除元素
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()函数:判断vector是否为空
vector<int> v;
bool isEmpty = v.empty(); // isEmpty的值为true
- resize()函数:改变数组大小
vector<int> v;
v.resize(3);
// v中包含3个元素,都是0
v.resize(5, 1);
// v中包含5个元素,前3个是0,后2个是1
2. deque
deque是一种双端队列容器,提供了以下常用方法:
- push_back()函数:在队列末尾插入元素
deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
// d中包含3个元素,分别是1, 2, 3
- push_front()函数:在队列头部插入元素
deque<int> d;
d.push_front(1);
d.push_front(2);
d.push_front(3);
// d中包含3个元素,分别是3, 2, 1
- pop_back()函数:弹出队列末尾元素
deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_back();
// d中包含2个元素,分别是1, 2
- pop_front()函数:弹出队列头部元素
deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_front();
// d中包含2个元素,分别是2, 3
- insert()函数:插入元素
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()函数:删除元素
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()函数:判断deque是否为空
deque<int> d;
bool isEmpty = d.empty(); // isEmpty的值为true
- resize()函数:改变队列大小
deque<int> d;
d.resize(3);
// d中包含3个元素,都是0
d.resize(5, 1);
// d中包含5个元素,前3个是0,后2个是1
3. set
set是一种集合容器,提供了以下常用方法:
- insert()函数:插入元素
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
// s中包含3个元素,分别是1, 2, 3
- erase()函数:删除元素
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.erase(2);
// s中包含2个元素,分别是1, 3
- find()函数:查找元素
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()函数:判断set是否为空
set<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true
- 自定义排序规则
如果需要按照自定义的排序规则来排序set中的元素,可以使用自定义比较函数或者自定义比较类。比如,如果想要按照从小到大的顺序排列元素,可以定义一个小于号运算符:
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是一种双向链表容器,提供了以下常用方法:
- push_back()函数:在链表末尾插入元素
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
// l中包含3个元素,分别是1, 2, 3
- push_front()函数:在链表头部插入元素
list<int> l;
l.push_front(1);
l.push_front(2);
l.push_front(3);
// l中包含3个元素,分别是3, 2, 1
- pop_back()函数:弹出链表末尾元素
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_back();
// l中包含2个元素,分别是1, 2
- pop_front()函数:弹出链表头部元素
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_front();
// l中包含2个元素,分别是2, 3
- erase()函数:删除元素
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()函数:判断list是否为空
list<int> l;
bool isEmpty = l.empty(); // isEmpty的值为true
- 自定义排序规则
list不支持自定义排序规则,因为它是一个链表而不是一个有序容器。如果需要按照自定义的排序规则来排序元素,可以考虑使用set、vector、deque等容器。
C++ STL中的stack和map是两种常用的容器,用于实现栈和映射。下面是它们的方法概述及举例:
5. stack
stack是一个后进先出(LIFO)的容器,提供了以下常用方法:
- push()函数:在栈顶插入元素
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
// 栈中元素为3,2,1
- pop()函数:弹出栈顶元素
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
// 栈中元素为2,1
- top()函数:访问栈顶元素
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
int x = s.top(); // x的值为3
- empty()函数:判断栈是否为空
stack<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true
6. map
map是一种键值对映射容器,提供了以下常用方法:
- insert()函数:插入键值对
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()函数:删除键值对
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()函数:查找键对应的值
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()函数:判断map是否为空
map<string, int> m;
bool isEmpty = m.empty(); // isEmpty的值为true
- 自定义排序规则
如果需要按照自定义的排序规则来排序map中的键,可以使用自定义比较函数或者自定义比较类。比如,如果想要按照从小到大的顺序排列键,可以定义一个小于号运算符:
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是一个先进先出(FIFO)的容器,提供了以下常用方法:
- push()函数:在队尾插入元素
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
// 队列中元素为1,2,3
- pop()函数:弹出队首元素
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.pop();
// 队列中元素为2,3
- front()函数:访问队首元素
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.front(); // x的值为1
- back()函数:访问队尾元素
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.back(); // x的值为3
- empty()函数:判断队列是否为空
queue<int> q;
bool isEmpty = q.empty(); // isEmpty的值为true
8. priority_queue
priority_queue是一个优先队列,每次弹出的元素是队列中优先级最高的元素。默认情况下,元素的优先级是按照从大到小的顺序排列的。priority_queue提供了以下常用方法:
- push()函数:在队尾插入元素
priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
// 队列中元素为3,2,1
- pop()函数:弹出队首元素
priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
pq.pop();
// 队列中元素为2,1
- top()函数:访问队首元素
priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
int x = pq.top(); // x的值为3
- empty()函数:判断队列是否为空
priority_queue<int> pq;
bool isEmpty = pq.empty(); // isEmpty的值为true
- 自定义排序规则
如果需要自定义元素的排序规则,可以通过重载运算符来实现。比如,如果想要按照从小到大的顺序排列元素,可以定义一个小于号运算符:
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
C++ STL中的string是一个类,表示一个可变长度的字符串。string类提供了许多函数方法,用于操作和处理字符串。下面是一些常用的string函数方法及其用法示例:
1. 构造函数
- 默认构造函数
string s1; // s1是一个空字符串
- 带参数的构造函数
string s2("hello"); // s2是一个字符串,内容为"hello"
string s3(5, 'a'); // s3是一个字符串,内容为"aaaaa"
2. 赋值操作
- 赋值运算符
string s1 = "hello";
string s2 = s1; // s2的内容和s1相同
string s3;
s3 = s1; // s3的内容和s1相同
- assign()函数
string s1 = "hello";
string s2;
s2.assign(s1); // s2的内容和s1相同
string s3;
s3.assign("world"); // s3的内容为"world"
3. 访问操作
- []运算符
string s = "hello";
char c = s[0]; // c的值为'h'
- at()函数
string s = "hello";
char c = s.at(0); // c的值为'h'
- front()函数
string s = "hello";
char c = s.front(); // c的值为'h'
- back()函数
string s = "hello";
char c = s.back(); // c的值为'o'
4. 插入操作
- push_back()函数
string s = "hello";
s.push_back('!'); // s的内容为"hello!"
- insert()函数
string s = "hello";
s.insert(0, "world"); // s的内容为"worldhello"
5. 删除操作
- pop_back()函数
string s = "hello!";
s.pop_back(); // s的内容为"hello"
- erase()函数
string s = "hello world";
s.erase(6, 5); // s的内容为"hello"
6. 查找操作
- find()函数
string s = "hello world";
int pos = s.find("world"); // pos的值为6
- rfind()函数
string s = "hello world";
int pos = s.rfind("l"); // pos的值为9
7. 替换操作
- replace()函数
string s = "hello world";
s.replace(6, 5, "you"); // s的内容为"hello you"
8. 子串操作
- substr()函数
string s = "hello world";
string sub = s.substr(0, 5); // sub的内容为"hello"
9. 其他操作
- length()函数
string s = "hello world";
int len = s.length(); // len的值为11
- size()函数
string s = "hello world";
int len = s.size(); // len的值为11
- empty()函数
string s = "hello world";
bool isEmpty = s.empty(); // isEmpty的值为false