日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++stl和std_std :: replace()函数以及C ++ STL中的示例

發布時間:2023/12/1 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++stl和std_std :: replace()函数以及C ++ STL中的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c++stl和std

C ++ STL std :: replace()函數 (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()函數是算法標頭的庫函數,用于在容器的給定范圍內用新值替換舊值,它接受指向開始和結束位置的迭代器,要替換的舊值以及要分配的新值。

Note: To use replace() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

注意:要使用replace()函數 –包括<algorithm>頭文件,或者您可以簡單地使用<bits / stdc ++。h>頭文件。

Syntax of std::replace() function

std :: replace()函數的語法

std::replace(iterator start, iterator end, const T& old_value, const T& new_value);

Parameter(s):

參數:

  • 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.

    迭代器開始,迭代器結束 –這些迭代器指向容器中我們必須運行替換操作的開始和結束位置。

  • 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 70

C ++ STL程序演示了std :: replace()函數的使用 (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.

在此程序中,我們有一個向量,并且正在將新值分配給舊值。

//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 70

Reference: C++ std::replace()

參考: C ++ std :: replace()

翻譯自: https://www.includehelp.com/stl/std-replace-function-with-example.aspx

c++stl和std

總結

以上是生活随笔為你收集整理的c++stl和std_std :: replace()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。