c++stl和std_std :: replace()函数以及C ++ STL中的示例
c++stl和std
C ++ STL std :: replace()函數(shù) (C++ STL std::replace() function)
replace() function is a library function of algorithm header, it is used to replace an old value with a new value in the given range of a container, it accepts iterators pointing to the starting and ending positions, an old value to be replaced and a new value to be assigned.
replace()函數(shù)是算法標(biāo)頭的庫函數(shù),用于在容器的給定范圍內(nèi)用新值替換舊值,它接受指向開始和結(jié)束位置的迭代器,要替換的舊值以及要分配的新值。
Note: To use replace() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.
注意:要使用replace()函數(shù) –包括<algorithm>頭文件,或者您可以簡單地使用<bits / stdc ++。h>頭文件。
Syntax of std::replace() function
std :: replace()函數(shù)的語法
std::replace(iterator start, iterator end, const T& old_value, const T& new_value);Parameter(s):
參數(shù):
iterator start, iterator end – these are the iterators pointing to the starting and ending positions in the container, where we have to run the replace operation.
迭代器開始,迭代器結(jié)束 –這些迭代器指向容器中我們必須運(yùn)行替換操作的開始和結(jié)束位置。
old_value – is the value to be searched and replaced with the new value.
old_value –是要搜索并替換為新值的值。
new_value – a value to be assigned instead of an old_value.
new_value –要分配的值,而不是old_value。
Return value: void – it returns noting.
返回值: void –返回注釋。
Example:
例:
Input:vector<int> v{ 10, 20, 10, 20, 10, 30, 40, 50, 60, 70 };//replacing 10 with 99replace(v.begin(), v.end(), 10, 99);Output:99 20 99 20 99 30 40 50 60 70C ++ STL程序演示了std :: replace()函數(shù)的使用 (C++ STL program to demonstrate use of std::replace() function)
In this program, we have a vector and we are assigning a new value to an old value.
在此程序中,我們有一個(gè)向量,并且正在將新值分配給舊值。
//C++ STL program to demonstrate use of //std::replace() function #include <iostream> #include <algorithm> #include <vector> using namespace std;int main() {//vectorvector<int> v{ 10, 20, 10, 20, 10, 30, 40, 50, 60, 70 };//printing vector elementscout << "before replacing, v: ";for (int x : v)cout << x << " ";cout << endl;//replacing 10 with 99replace(v.begin(), v.end(), 10, 99);//printing vector elementscout << "after replacing, v: ";for (int x : v)cout << x << " ";cout << endl;return 0; }Output
輸出量
before replacing, v: 10 20 10 20 10 30 40 50 60 70 after replacing, v: 99 20 99 20 99 30 40 50 60 70Reference: C++ std::replace()
參考: C ++ std :: replace()
翻譯自: https://www.includehelp.com/stl/std-replace-function-with-example.aspx
c++stl和std
總結(jié)
以上是生活随笔為你收集整理的c++stl和std_std :: replace()函数以及C ++ STL中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “池中赤鲤庖所捐”下一句是什么
- 下一篇: 固原治疗精子活率低最好的医院推荐