replace 的用法
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/26 11:04:34
replace 的用法
replace 的用法
replace 的用法
replace是STL 算法中的一种,其用法如下:
Examines each element in a range and replaces it if it matches a specified value.
template
void replace(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _OldVal,
const Type& _NewVal
);
Parameters
_First
A forward iterator pointing to the position of the first element in the range from which elements are being replaced.
_Last
A forward iterator pointing to the position one past the final element in the range from which elements are being replaced.
_OldVal
The old value of the elements being replaced.
_NewVal
The new value being assigned to the elements with the old value.
Remarks
The range referenced must be valid; all pointers must be dereferenceable and within the sequence the last position is reachable from the first by incrementation.
The order of the elements not replaced remains stable.
The operator== used to determine the equality between elements must impose an equivalence relation between its operands.
The complexity is linear; there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.
Example
Copy Code
// alg_replace.cpp
// compile with: /EHsc
#include
#include
#include
int main( ) {
using namespace std;
vector v1;
vector ::iterator Iter1;
int i;
for ( i = 0 ; i