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 their content. (Sizes of both of the vectors may differ).

vector :: swap()“ vector”頭文件的庫函數,用于交換向量的內容,用向量調用它并接受另一個向量作為參數并交換其內容。 (兩個向量的大小可能不同)。

Note: To use vector, include <vector> header.

注意:要使用向量,請包含<vector>標頭。

Syntax of vector::swap() function

vector :: swap()函數的語法

    vector::swap(vector& v);

Parameter(s): v – It is another vector to be swapped content with current vector.

參數: v –這是另一個要與當前向量交換內容的向量。

Return value: void – It returns nothing.

返回值: void –不返回任何內容。

Example:

例:

    Input:
vector<int> v1{ 10, 20, 30, 40, 50 };
vector<int> v2{ 100, 200, 300 };
//swapping content of the vectors
v1.swap(v2);
Output:
//if we print the values
v1: 100 200 300
v2: 10 20 30 40 50

C ++程序演示vector :: swap()函數的示例 (C++ program to demonstrate example of vector::swap() function)

//C++ STL program to demonstrate example of
//vector::erase() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50 };
vector<int> v2{ 100, 200, 300 };
//printing the sizes and values of the vectors
cout << "before swap() call..." << endl;
cout << "size of v1: " << v1.size() << endl;
cout << "size of v2: " << v2.size() << endl;
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
cout << "v2: ";
for (int x : v2)
cout << x << " ";
cout << endl;
//swapping the content of the vectors
v1.swap(v2);
//printing the sizes and values of the vectors
cout << "after swap() call..." << endl;
cout << "size of v1: " << v1.size() << endl;
cout << "size of v2: " << v2.size() << endl;
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
cout << "v2: ";
for (int x : v2)
cout << x << " ";
cout << endl;
return 0;
}

Output

輸出量

before swap() call...
size of v1: 5
size of v2: 3
v1: 10 20 30 40 50
v2: 100 200 300
after swap() call...
size of v1: 3
size of v2: 5
v1: 100 200 300
v2: 10 20 30 40 50

Reference: C++ vector::swap()

參考: C ++ vector :: swap()

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

stl swap函數

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

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

相關文章

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;位置和灰度。圖像數字化一般分為采樣、量化與編碼三個步驟。數…

三、“滌綸纖維和棉纖維兩組分纖維在滌/棉混紡織物燃燒過程中有著明顯的物理相互作用和化學相互作用”,解釋這兩種作用。

“滌綸纖維和棉纖維兩組分纖維在滌/棉混紡織物燃燒過程中有著明顯的物理相互作用和化學相互作用”,解釋這兩種作用。 收集資料階段 棉纖維燃燒后炭化,而滌綸燃燒時熔融滴落,由于棉纖維成為支持體,可使熔融纖維聚集,并阻止其滴落,使熔融纖維燃燒更加劇烈,即所謂"支架效應…

關于性能測試的通俗解釋

關于性能測試的通俗解釋: http://www.docin.com/p-645879730.html 轉載于:https://www.cnblogs.com/preftest/archive/2013/05/03/3057231.html

oracle marley,滾石雜志500大專輯,對歐美音樂感興趣的可以找來聽聽。

滾石雜志于2003年11月評選出的滾石雜志五百大專輯。值得一提的是&#xff0c;披頭士樂隊占據了前五中的三席&#xff0c;前五十中的7席&#xff0c;正式出版的專輯幾乎全部入選前五百。排名 演唱者 專輯001 披頭士樂隊(The Beatles) Sgt. Peppers Lonely Hearts Club Band002 海…

ci中使用smarty

<p>d</p> <?php defined(BASEPATH) or die(Access restricted!);/** 作者&#xff1a;晉勇*/require(APPPATH.libraries/smarty/Smarty.class.php);classCismarty extendsSmarty{public$exttpl;public$dir;public$layoutlayout/main;/** * 構造函數 * * access…

kinect中psi是什么_PSI的完整形式是什么?

kinect中psi是什么PSI&#xff1a;每平方英寸磅/國際人口服務 (PSI: Pound per Square Inch / Population Services International) 1)PSI&#xff1a;每平方英寸磅 (1) PSI: Pound per Square Inch) PSI is an abbreviation of Pound per Square Inch. Pound per Square Inch …

jupyter notebook指定工作目錄

【1】打開Anaconda Navigator 打開Anaconda Navigator&#xff0c;點擊左側Environments&#xff0c;點擊base(root)->open Terminal 【2】輸入指令jupyter notebook --generate-config 按下回車鍵&#xff0c;彈出config所在位置。 以VS Code打開文件 【3】修改第26…

四、纖維素纖維使用P-N系阻燃劑協同作用的原理?

纖維素纖維使用P-N系阻燃劑協同作用的原理? 收集資料階段 某些含氮、磷化合物可以增強化合物的阻燃性,原因就是磷、氮在反應過程中形成含 N-P 鍵的中間體,可以改善羰基反應活性和磷酰化速率,進而提高成炭率;另一個重要的原因是氮化合物可以延緩凝聚相中含磷化合物的會揮發…