C++ 修改序列操作
在C++的STL(标准模板库)中,修改序列操作是一组能够修改容器内元素的算法。这些算法可以帮助我们对序列进行复制、替换、填充、删除等操作,大大简化了对容器内元素的处理。本文将详细介绍这些修改序列操作的使用方法和实际应用场景。
修改序列操作概述
修改序列操作主要位于<algorithm>
头文件中,它们接受迭代器作为参数,对迭代器指向的序列进行修改。常用的修改序列操作包括:
- 复制操作:
copy
,copy_if
,copy_n
,copy_backward
等 - 替换操作:
replace
,replace_if
,replace_copy
等 - 填充操作:
fill
,fill_n
,generate
,generate_n
等 - 移除操作:
remove
,remove_if
,remove_copy
等 - 独特化操作:
unique
,unique_copy
等 - 交换和重排操作:
swap
,iter_swap
,swap_ranges
,reverse
,rotate
等
复制操作
复制操作是最基础的序列修改操作,用于将一个序列的元素复制到另一个序列中。
copy
copy
算法将一个范围内的元素复制到另一个位置:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(5);
// 将source中的元素复制到destination中
std::copy(source.begin(), source.end(), destination.begin());
// 打印destination中的元素
for (int num : destination) {
std::cout << num << " ";
}
// 输出: 1 2 3 4 5
return 0;
}
copy_if
copy_if
算法根据指定条件将元素复制到另一个位置:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> destination;
// 预留空间以提高效率
destination.reserve(source.size());
// 将source中的偶数复制到destination中
std::copy_if(source.begin(), source.end(),
std::back_inserter(destination),
[](int n) { return n % 2 == 0; });
// 打印destination中的元素
for (int num : destination) {
std::cout << num << " ";
}
// 输出: 2 4 6 8 10
return 0;
}
提示
使用std::back_inserter
创建一个插入迭代器,每次赋值时都会在容器末尾插入新元素,避免了预先分配空间大小的问题。
替换操作
替换操作用于将序列中满足特定条件的元素替换为新值。
replace
replace
算法将序列中所有等于给定值的元素替换为新值:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 2, 5, 2, 7, 8};
// 将所有2替换为20
std::replace(numbers.begin(), numbers.end(), 2, 20);
// 打印替换后的结果
for (int num : numbers) {
std::cout << num << " ";
}
// 输出: 1 20 3 20 5 20 7 8
return 0;
}
replace_if
replace_if
算法将序列中所有满足谓词条件的元素替换为新值:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
// 将所有偶数替换为0
std::replace_if(numbers.begin(), numbers.end(),
[](int n) { return n % 2 == 0; }, 0);
// 打印替换后的结果
for (int num : numbers) {
std::cout << num << " ";
}
// 输出: 1 0 3 0 5 0 7 0
return 0;
}
填充操作
填充操作用于将特定值或者根据生成器函数的结果填充到序列中。
fill
fill
算法将指定的值填充到指定范围的每个元素:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers(5);
// 将向量中的所有元素设置为10
std::fill(numbers.begin(), numbers.end(), 10);
// 打印结果
for (int num : numbers) {
std::cout << num << " ";
}
// 输出: 10 10 10 10 10
return 0;
}
generate
generate
算法将生成器函数的结果填充到指定范围的每个元素:
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
int main() {
std::vector<int> numbers(5);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 100);
// 用随机数填充向量
std::generate(numbers.begin(), numbers.end(), [&]() { return dis(gen); });
// 打印结果
for (int num : numbers) {
std::cout << num << " ";
}
// 输出: 例如 45 23 78 91 12 (随机数)
return 0;
}
移除操作
移除操作用于从序列中移除满足特定条件的元素。
警告
STL的移除操作并不会真正从容器中删除元素,它们只是将要保留的元素移动到序列的前部,并返回一个迭代器指向新的逻辑结尾。要真正删除元素,需要配合容器的erase