C++学习笔记---vector容器使用教程

利用 remove_if()erase()vector 中移除指定 set 中包含的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <unordered_set>
std::unordered_set<std::string> replace_str_set{",", "."};
bool IsReplaceStr(std::string str) {
if (replace_str_set.find(str) != replace_str_set.end()) {
return true;
}
return false;
}
int main() {
std::vector<std::string> string_vector{"a", ",", "b", ".", "c"};
std::vector<std::string>::iterator it;
std::cout << "before replace: ";
for (it = string_vector.begin(); it != string_vector.end(); it++) {
std::cout << *it; << std::endl;
}
std::cout << std::endl;
string_vector.erase(remove_if(string_vector.begin(), string_vector.end(), IsReplaceStr), string_vector.end());
std::cout << "after replace: ";
for (it = string_vector.begin(); it != string_vector.end(); it++) {
std::cout << *it;
}
std::cout << std::endl;
return 0;
}

输出结果:

1
2
before replace: a,b.c
after replace: abc

-------------本文结束感谢您的阅读-------------
坚持整理学习笔记,您的支持将鼓励我继续整理下去!