C++ STL 中的運算元 operator= 集合
函式 operator= 用於集合中複製一個集合(或在 C++ STL 中移動到另一個集合)。它對集合的行為就如同常規“=”賦值操作一樣。此函式有三種過載形式 −
複製 :- set& operator= (const set& s1) −
此函式將集合 s1 中的所有元素複製到另一個集合。傳遞的引數是相同型別的集合。
用法 − set s1=s2;
移動 :- set& operator=( set &&s1 ) −
此函式將集合 s1 的元素移動到呼叫方集合中。
初始化器列表 :- set& operator= (initializer_list<value_type> ilist) −
此版本將初始化器列表 ilist 的值複製到呼叫方集合中。
用法 − set<int> s1= { 1,2,3,4,5 };
注意 − 它們全部返回 set<T> 型別的此指標的引用。
以下程式用於演示在 C++ 程式中使用 round 函式 −
示例
#include <iostream>
#include <set>
using namespace std;
// merge function
int main(){
set<int> set1, set2;
// List initialization
set1 = { 1, 2, 3, 4, 5 };
set2 = { 10,11,12,13 };
// before copy
cout<<"set1 :";
for (auto s = set1.begin(); s != set1.end(); ++s) {
cout << *s << " ";
}
cout << endl;
cout<<"set2 :";
for (auto s = set2.begin(); s != set2.end(); ++s) {
cout << *s << " ";
}
//after copy set1 to set2
cout<<endl<<"After Copy"<<endl;
cout<<"set1 :";
set1=set2;
for (auto s = set1.begin(); s != set1.end(); ++s) {
cout << *s << " ";
}
return 0;
}輸出
set1 :1 2 3 4 5 set2 :10 11 12 13 After Copy set1 :10 11 12 13
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP