C++ 算法 replace_copy_if() 函數用於將範圍 [first, last) 複製到從結果開始的範圍,將 pred 返回 true 的那些放置在 new_value 處。它使用謂詞 pred 而不是 operator== 來比較元素。
此函數檢查源範圍中的每個元素,並在將結果複製到新的目標範圍時如果滿足指定的謂詞,則替換它。
用法
template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
OutputIterator replace_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred, const T& new_value);
參數
first:一個輸入迭代器,指向元素被替換的範圍內的初始位置。
last:一個輸入迭代器,指向元素被替換的範圍內的最終位置。
result:一個輸出迭代器,指向存儲結果序列的範圍的第一個元素。
pred:如果要替換元素的值,則必須滿足的一元謂詞。
old_value:被替換元素的舊值。
new_value:分配給具有舊值的元素的新值。
返回值
replace_copy_if() 函數返回一個輸出迭代器,該迭代器指向指向結果序列中寫入的最後一個元素的位置。
複雜度
複雜度與 first 和 last 之間的距離呈線性:應用 pred 並為每個元素執行賦值。
數據競爭
訪問範圍 [first1, last1) 中的對象。
結果和返回值範圍內的對象被修改。
異常安全
如果 pred、元素賦值或迭代器上的操作中的任何一個拋出異常,則此函數將拋出異常。
請注意,無效參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 replace_copy_if() 的使用:
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main() {
vector<int> v = { 3,1,2,1,2 };
replace_copy_if(v.begin(), v.end(),
ostream_iterator<int>(cout, ","),
[](int x) { return x%2 != 0; }, 10);
return 0;
}
輸出:
10,10,2,10,2,
例子2
讓我們看另一個簡單的例子:
// CPP code to illustrate
// replace_copy_if
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Function to check if number is even
int IsEven(int i)
{
return ((i % 2) == 0);
}
// Function to print content of vector
void print(vector<int>& v)
{
int len = v.size();
for (int i=0; i < len; i++)
cout << v[i] << " ";
cout << endl;
}
// Function to replace all even numbers from vector v1 and
// copying them to v2
void replace_copy_ifDemo(vector<int>& v1, vector<int>& v2)
{
replace_copy_if(v1.begin(), v1.end(), v2.begin(), IsEven, 0);
}
// Driver Code
int main()
{
vector<int> v1, v2;
for (int i = 1; i <= 10; i++)
v1.push_back(i);
cout << "Before replace_copy_if:";
print(v1);
v2.resize(v1.size()); // allocate space
replace_copy_ifDemo(v1, v2);
cout << "After replace_copy_if:";
print(v2);
return 0;
}
輸出:
Before replace_copy_if:1 2 3 4 5 6 7 8 9 10 After replace_copy_if:1 0 3 0 5 0 7 0 9 0
例子3
讓我們看另一個簡單的例子:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
const int MAX_ELEMENTS = 8 ;
// Define a template class vector of integers
typedef vector<int > IntVector ;
//Define an iterator for template class vector of integer
typedef IntVector::iterator IntVectorIt ;
//vector containing numbers
IntVector Numbers(MAX_ELEMENTS), Result(MAX_ELEMENTS) ;
IntVectorIt start, end, it, last, resultIt ;
//Initialize vector Numbers
Numbers[0] = 10 ;
Numbers[1] = 20 ;
Numbers[2] = 10 ;
Numbers[3] = 15 ;
Numbers[4] = 12 ;
Numbers[5] = 7 ;
Numbers[6] = 9 ;
Numbers[7] = 10 ;
start = Numbers.begin() ; // location of first
// element of Numbers
end = Numbers.end() ; // one past the location
// last element of Numbers
resultIt = Result.begin() ; // location of first
// element of Result
// print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// copy all elements from Numbers to Result
// replacing any item that >= 10 with 30
last = replace_copy_if(start, end, resultIt,
bind2nd(greater_equal<int>(), 10), 30) ;
//print number of elements copied to Result
cout << "Total number of elements copied to Result = "
<< last - resultIt << endl ;
start = Result.begin() ; // location of first
// element of Result
end = Result.end() ; // one past the location
// last element of Result
// print content of Result
cout << "Result { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
輸出:
Numbers { 10 20 10 15 12 7 9 10 } Total number of elements copied to Result = 8 Result { 30 30 30 30 30 7 9 30 }
示例 4
讓我們看另一個簡單的例子:
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
bool greater6 ( int value ) {
return value >6;
}
int main( ) {
using namespace std;
vector <int> v1;
list <int> L1 (13);
vector <int>::iterator Iter1;
list <int>::iterator L_Iter1;
int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );
random_shuffle ( v1.begin( ), v1.end( ) );
int iii;
for ( iii = 0 ; iii <= 13 ; iii++ )
v1.push_back( 1 );
cout << "The original vector v1 is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Replace elements with a value of 7 in the 1st half of a vector
// with a value of 70 and copy it into the 2nd half of the vector
replace_copy_if ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -14,
greater6 , 70);
cout << "The vector v1 with values of 70 replacing those greater"
<< "\n than 6 in the 1st half & copied into the 2nd half is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Replace elements in a vector with a value of 70
// with a value of 1 and copy into a list
replace_copy_if ( v1.begin( ), v1.begin( ) + 13,L1.begin( ),
greater6 , -1 );
cout << "A list copy of vector v1 with the value -1\n replacing "
<< "those greater than 6 is:\n ( " ;
for ( L_Iter1 = L1.begin( ) ; L_Iter1 != L1.end( ) ; L_Iter1++ )
cout << *L_Iter1 << " ";
cout << ")." << endl;
}
輸出:
The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ). The vector v1 with values of 70 replacing those greater than 6 in the 1st half & copied into the 2nd half is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 4 70 70 70 0 5 70 1 6 70 3 70 70 2 ). A list copy of vector v1 with the value -1 replacing those greater than 6 is: ( 4 -1 -1 -1 0 5 -1 1 6 -1 3 -1 -1 ).
相關用法
- C++ Algorithm replace_copy()用法及代碼示例
- C++ Algorithm replace_if()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm reverse_copy()用法及代碼示例
- C++ Algorithm remove_copy()用法及代碼示例
- C++ Algorithm reverse()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm rotate()用法及代碼示例
- C++ Algorithm rotate_copy()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm replace_copy_if()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。