一 概述
- C++ 标准库中提供了很多算法,定义于头文件 < algorithm >。本文主要探究以下用于 替换区间元素 的算法:
- std::replace 替换区间中特定值元素为另一个值
- std::replace_if 替换区间中满足特定判别标准的元素为另一个值
- std::replace_copy 复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值
- std::replace_copy_if 复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值
二 辅助函数
三 std::replace
template< class ForwardIt, class T >
void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );(1)(C++20 前)
template< class ForwardIt, class T >
constexpr void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );(1)(C++20 起)
template< class ExecutionPolicy, class ForwardIt, class T >
void replace( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );(2) (C++17 起)
if (1) {
std::vector<int> vc;
fill(vc, 1, 9);
print("before replace vc: ", vc);
std::replace(vc.begin(), vc.end(), 5, 500);
print("after replace vc: ", vc);
}
before replace vc: 1 2 3 4 5 6 7 8 9
after replace vc: 1 2 3 4 500 6 7 8 9
四 std::replace_if
template< class ForwardIt, class UnaryPredicate, class T >
void replace_if( ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );(1)(C++20 前)
template< class ForwardIt, class UnaryPredicate, class T >
constexpr void replace_if( ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );(1)(C++20 起)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate, class T >
void replace_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );(2)(C++17 起)
if (1) {
std::vector<int> vc;
fill(vc, 1, 9);
print("before replace_if vc: ", vc);
std::replace_if(vc.begin(), vc.end(), even, 500);
print("after replace_if vc: ", vc);
}
before replace_if vc: 1 2 3 4 5 6 7 8 9
after replace_if vc: 1 500 3 500 5 500 7 500 9
五 std::replace_copy
template< class InputIt, class OutputIt, class T >
OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,
const T& old_value, const T& new_value );(1)(C++20 前)
template< class InputIt, class OutputIt, class T >
constexpr OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,
const T& old_value, const T& new_value );(1)(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 replace_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, const T& old_value, const T& new_value );(2)(C++17 起)
if (1) {
std::vector<int> vc1;
fill(vc1, 1, 9);
std::vector<int> vc2;
print("before replace_copy vc1: ", vc1);
print("before replace_copy vc2: ", vc2);
std::replace_copy(vc1.begin(), vc1.end(), std::back_inserter(vc2), 5,
500);
print("after replace_copy vc1: ", vc1);
print("after replace_copy vc2: ", vc2);
}
before replace_copy vc1: 1 2 3 4 5 6 7 8 9
before replace_copy vc2:
after replace_copy vc1: 1 2 3 4 5 6 7 8 9
after replace_copy vc2: 1 2 3 4 500 6 7 8 9
六 std::replace_copy_if
template< class InputIt, class OutputIt, class UnaryPredicate, class T >
OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value );(1)(C++20 前)
template< class InputIt, class OutputIt, class UnaryPredicate, class T >
constexpr OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value );(1)(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate, class T >
ForwardIt2 replace_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, UnaryPredicate p, const T& new_value );(2)(C++17 起)
if (1) {
std::vector<int> vc1;
fill(vc1, 1, 9);
std::vector<int> vc2;
print("before replace_copy_if vc1: ", vc1);
print("before replace_copy_if vc2: ", vc2);
std::replace_copy_if(vc1.begin(), vc1.end(), std::back_inserter(vc2),
even, 500);
print("after replace_copy_if vc1: ", vc1);
print("after replace_copy_if vc2: ", vc2);
}
before replace_copy_if vc1: 1 2 3 4 5 6 7 8 9
before replace_copy_if vc2:
after replace_copy_if vc1: 1 2 3 4 5 6 7 8 9
after replace_copy_if vc2: 1 500 3 500 5 500 7 500 9
七 参考