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 99
replace(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()
{
//vector
vector<int> v{ 10, 20, 10, 20, 10, 30, 40, 50, 60, 70 };
//printing vector elements
cout << "before replacing, v: ";
for (int x : v)
cout << x << " ";
cout << endl;
//replacing 10 with 99
replace(v.begin(), v.end(), 10, 99);
//printing vector elements
cout << "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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378481.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378481.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378481.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《Two Dozen Short Lessons in Haskell》(二十)分數

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻譯為Haskell二十四學時教程&#xff0c;該書如果不用于贏利&#xff0c;可以任意發布&#xff0c;但需要保留他們的copyright&#xff09;這本書是學習 Ha…

oracle 查詢不能重復,oracle – 如何防止在選擇查詢中選擇重復行?

我被賦予了從Oracle數據庫中選擇關鍵數據的任務,但我注意到我的select正在返回重復的行.我不需要它們用于我的報告但我不希望它們刪除它們.有人可以幫助只獲取我需要的數據.我嘗試了以下代碼,但這沒有用.SELECT distinct bbp.SUBCAR "Treadwell",bbp.BATCH_ID "…

數字圖像課程工程大作業分析

試題分析&#xff1a; 在連續的視頻中對火焰及水柱的軌跡檢測&#xff0c;效果如圖。 ** 提示&#xff1a; 1、火焰可利用亮度和顏色 2、水柱的軌跡需要先用背景差分獲得水柱的連通域&#xff0c;然后利用連通域上的像素點進行曲線的擬合&#xff0c;水槍的位置視為已知&#…

設計電子商務網站的10個技巧(轉自ITEye)

導讀&#xff1a;隨著先進科學技術的應用&#xff0c;人們無需外出逛幾個小時來“獵”東西&#xff0c;直接坐在家里就可以購買所需商品&#xff0c;支付服務費用。你只需一臺電腦就能搞定。人們習慣了周到的服務和漂亮的櫥窗&#xff0c;對網店的選擇也不例外。因此&#xff0…

分析酸對酸性染料染羊毛染色性能的影響?舉例說明酸性染料染羊毛時,如何選擇合適的染浴pH值?并說明原因。

分析酸對酸性染料染羊毛染色性能的影響?舉例說明酸性染料染羊毛時,如何選擇合適的染浴pH值?并說明原因。 標準答案: 羊毛屬于蛋白質纖維,屬于兩性纖維,酸影響羊毛的解離程度,帶電性及帶電量,影響熱力學性能。等電點以下,羊毛帶正電荷,與陰離子酸性染料之間存在靜電引…

ORACLE連接數據庫(備忘)

常用命令&#xff1a; conn sys/密碼 as sysdba 連接數據庫轉載于:https://www.cnblogs.com/jiangu66/archive/2013/05/01/3053787.html

stl swap函數_vector :: swap()函數以及C ++ STL中的示例

stl swap函數C vector :: swap()函數 (C vector::swap() function) vector::swap() is a library function of "vector" header, it is used to swap the content of the vectors, it is called with a vector and accepts another vector as an argument and swaps…

C++語法:vector的使用

【1】vector的創建與元素插入【2】vector元素的訪問【3】vector的基本使用技巧【4】vector的幾個重要操作【1】vector的創建與元素插入 std::vector<cv::Point> points; //vector容器中保存的類型是Point for (int i 0;i < 10;i) {float x rng.uniform(0, img.cols…

一、經含氟防水劑整理的織物主要存在的不足?

經含氟防水劑整理的織物主要存在的不足? 收集資料階段 含氟防水劑有哪些優缺點 一、含氟防水劑的優點 1、防水效果好,等級高。而無氟防水劑效果相對來說要差一些; 2、兼具防油的功能。無氟防水劑是不具備防油功能的; 3、穩定性好、與其他助劑的配伍好,工藝適應性強;有機…

Apache Web Login Authentication

Apache Web Login Authentication: Adding password protection to a web site using Apache web server authentication. AuthLDAPURL ldap://ldap.your-domain.com:389/ostooges?uid?subAuthLDAPBindDN "cnStoogeAdmin,ostooges"AuthLDAPBindPassword secret1Aut…

oracle中的with的用法,oracle中with子句的用法(轉)

語法&#xff1a;WITH query_name AS (subquery)[, query_name AS (subquery) ]...使用在主select關鍵字前&#xff0c;oracle將其當做一個內聯視圖或者臨時表使用。例子&#xff1a;1.最簡單的使用方法&#xff1a;如查詢部門名稱包含“A”的所有員工信息--with clausewith a …

模擬一個排隊系統

現在有一個數據源&#xff0c;有兩種狀態&#xff08;ON OFF&#xff09;&#xff0c;ON的持續時間服從均值為T_on的指數分布&#xff0c;OFF的持續時間服從均值為T_off的指數分布&#xff0c;源只在ON的時候產生數據包&#xff0c;服從均值為λ的指數分布 模擬一個排隊系統 每…

stl中copy()函數_std :: copy_if()函數以及C ++ STL中的示例

stl中copy()函數C STL std :: copy_if()函數 (C STL std::copy_if() function) copy_if() function is a library function of algorithm header, it is used to copy the elements of a container, it copies the certain elements (which satisfy the given condition) of a…

C++語法:求vector中的最大值及其位置

代碼&#xff1a; #include <iostream> #include <vector> #include <algorithm> using namespace std;int main(){vector<int> a { 2,4,6,7,1,0,8,9,6,3,2 };auto maxPosition max_element(a.begin(), a.end());cout << *maxPosition <&l…

二、織物具備超級防水效果的條件?

織物具備超級防水效果的條件? 收集資料階段 蓮花效應(Lotus Effect),指蓮葉表面具有超疏水(superhydrophobicity)以及自潔(self-cleaning)的特性。 由于蓮葉具有疏水、不吸水的表面,落在葉面上的雨水會因表面張力的作用形成水珠,換言之,水與葉面的接觸角(contacta…

C#編碼簡單性之函數篇(如何編寫簡短的C#代碼,隨時更新)

作者&#xff1a;陳勇出處&#xff1a;blog.csdn.net/cheny_com這是編碼簡單性系列中的其中一篇&#xff0c;之前幾篇包括代碼篇和語義篇。因為要積累案例&#xff0c;會隨時更新。之前提到&#xff1a;編碼簡單性的“心法”就是&#xff1a;只要屏幕上有任何兩部分代碼看上去相…

R學習筆記(1):R是什么

本文最新版已更新至http://thinkinside.tk/2013/05/03/r_notes_1_what.html 在學習量化投資的時候&#xff0c;我發現了R&#xff08;www.r-project.org&#xff09;。R到底是什么呢&#xff1f;在開始之前&#xff0c;先看看R的神奇之處。 1. R初窺 從CRAN&#xff08;The Co…

oracle網卡,Oracle_bond網卡配置

***************************loyu*******************************************************雙網卡創建bond虛擬網卡實驗*************************cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << EofDEVICEbond0BOOTPROTOstaticONBOOTyesIPADDR172.16.116.60N…

Python | 展示一個break語句示例

break is a keyword in python just like another programming language and it is used to break the execution of loop statement. 就像另一種編程語言一樣&#xff0c; break是python中的關鍵字&#xff0c;用于中斷loop語句的執行。 In the given example, loop is runni…

數字圖像處理知識總結

一&#xff1a;基本概念 數字圖像&#xff1a;指由被稱作像素的小塊區域組成的二維矩陣。將物理圖像行列劃分后&#xff0c;每個小塊區域稱為像素&#xff08;pixel&#xff09;。每個像素包括兩個屬性&#xff1a;位置和灰度。圖像數字化一般分為采樣、量化與編碼三個步驟。數…