在日常的开发中,常涉及到容器的常见操作,如查找、删除、排序等,C++ STL提供了丰富的算法库,可以方便的完成这些操作。为了避免重复造轮子,同时为了提高效率,了解常见的STL算法是非常有必要的。本文将介绍增删改相关算法。
1. std::fill
功能:用指定的值填充范围。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec(5);
std::fill(vec.begin(), vec.end(), 1);
for (auto i : vec) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
std::fill_n
功能:用指定的值填充范围的前n个元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec(5);
std::fill_n(vec.begin(), 3, 1);
for (auto i : vec) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
2.std::generate
功能:用指定的函数生成值填充范围。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec(5);
std::generate(vec.begin(), vec.end(), [n = 0]() mutable { return n++; });
for (auto i : vec) {
std::cout << i << " "; // 0 1 2 3 4
}
std::cout << std::endl;
return 0;
}
std::generate_n
功能:用指定的函数生成值填充范围的前n个元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec(5);
std::generate_n(vec.begin(), 3, [n = 0]() mutable { return n++; });
for (auto i : vec) {
std::cout << i << " "; // 0 1 2
}
std::cout << std::endl;
return 0;
}
3. std::transform
功能:将范围中的元素通过某种规则转换为另一个范围中的元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2(5);
std::transform(vec1.begin(), vec1.end(), vec2.begin(), [](int i) { return i 2; });
for (auto i : vec2) {
std::cout << i << " "; // 2 4 6 8 10
}
std::cout << std::endl;
return 0;
}
4. std::copy
功能:将范围中的元素复制到另一个范围中。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2(5);//预分配空间
std::copy(vec1.begin(), vec1.end(), vec2.begin());//vec2为目标范围
for (auto i : vec2) {
std::cout << i << " "; // 1 2 3 4 5
}
std::cout << std::endl;
return 0;
}
std::copy_n
功能:将范围中的前n个元素复制到另一个范围中。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2(5);//预分配空间
std::copy_n(vec1.begin(), 3, vec2.begin());//vec2为目标范围
for (auto i : vec2) {
std::cout << i << " "; // 1 2 3
}
std::cout << std::endl;
return 0;
}
std::copy_if
功能:将范围中满足条件的元素复制到另一个范围中。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2(5);//预分配空间
std::copy_if(vec1.begin(), vec1.end(), vec2.begin(), [](int i) { return i % 2 == 0; });//vec2为目标范围
for (auto i : vec2) {
std::cout << i << " "; // 2 4
}
std::cout << std::endl;
return 0;
}
5. std::remove
功能:删除范围中等于指定值的元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
vec.erase(std::remove(vec.begin(), vec.end(), 3), vec.end());
for (auto i : vec) {
std::cout << i << " "; // 1 2 4 5
}
std::cout << std::endl;
return 0;
}
std::remove_if
功能:删除范围中满足条件的元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
vec.erase(std::remove_if(vec.begin(), vec.end(), [](int i) { return i % 2 == 0; }), vec.end());
for (auto i : vec) {
std::cout << i << " "; // 1 3 5
}
std::cout << std::endl;
return 0;
}
6. std::replace
功能:将范围中等于指定值的元素替换为另一个值。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
std::replace(vec.begin(), vec.end(), 3, 6);
for (auto i : vec) {
std::cout << i << " "; // 1 2 6 4 5
}
std::cout << std::endl;
return 0;
}
std::replace_if
功能:将范围中满足条件的元素替换为另一个值。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
std::replace_if(vec.begin(), vec.end(), [](int i) { return i % 2 == 0; }, 6);
for (auto i : vec) {
std::cout << i << " "; // 1 6 3 6 5
}
std::cout << std::endl;
return 0;
}
7. std::for_each
功能:对范围中的每个元素执行指定的操作。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
std::for_each(vec.begin(), vec.end(), [](int& i) { i = 2; });//对每个元素进行操作
for (auto i : vec) {
std::cout << i << " "; // 2 4 6 8 10
}
std::cout << std::endl;
return 0;
}
8. std::reverse
功能:将范围中的元素顺序反转。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
std::reverse(vec.begin(), vec.end());
for (auto i : vec) {
std::cout << i << " "; // 5 4 3 2 1
}
std::cout << std::endl;
return 0;
}
9. std::swap
功能:交换两个范围中的元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2{ 6, 7, 8, 9, 10 };
std::swap(vec1, vec2);
for (auto i : vec1) {
std::cout << i << " "; // 6 7 8 9 10
}
std::cout << std::endl;
return 0;
}
std::swap_ranges
功能:交换两个范围中的元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<int> vec2{ 6, 7, 8, 9, 10 };
std::swap_ranges(vec1.begin(), vec1.end(), vec2.begin());
for (auto i : vec1) {
std::cout << i << " "; // 6 7 8 9 10
}
}
10. std::rotate
功能:将范围中的元素向右旋转指定的步数。
template <class ForwardIterator>
void rotate (ForwardIterator first, ForwardIterator middle,
ForwardIterator last);
参数
代码示例:
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
///
std::rotate(vec.begin(), vec.begin() + 2, vec.end());
for (auto i : vec) {
std::cout << i << " "; // 3 4 5 1 2
}
std::cout << std::endl;
return 0;
}
11. std::unique
功能:删除范围中连续的重复元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 2, 3, 4, 4, 4, 5 };
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
for (auto i : vec) {
std::cout << i << " "; // 1 2 3 4 5
}
std::cout << std::endl;
return 0;
}
std::unique_copy
功能:将范围中的元素复制到另一个范围,并删除连续的重复元素。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 2, 3, 4, 4, 4, 5 };
std::vector<int> vec2(vec.size());
std::unique_copy(vec.begin(), vec.end(), vec2.begin());
for (auto i : vec2) {
std::cout << i << " "; // 1 2 3 4 5
}
std::cout << std::endl;
return 0;
}
12. std::clamp
功能:将值限制在指定范围内。小于下限的值将被替换为下限,大于上限的值将被替换为上限。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
std::transform(vec.begin(), vec.end(), vec.begin(), [](int i) { return std::clamp(i, 2, 4); });
for (auto i : vec) {
std::cout << i << " "; // 2 2 3 4 4
}
std::cout << std::endl;
return 0;
}
13. std::accumulate
功能:计算范围中元素的和。
include <algorithm>
include <vector>
include <iostream>
int main() {
std::vector<int> vec{ 1, 2, 3, 4, 5 };
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << sum << std::endl; // 15
return 0;
}