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