C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Set operator=

C++ Set operator=

Set中 operator=有以下三种用法:
operator=用于通过替换旧内容(或复制内容)将新内容分配给已 Set 的容器,并在必要时修改大小。 operator=用于将一个容器的内容移动到另一个容器中,并在必要时修改大小。 operator=用于复制初始化器列表中的元素以 Set 容器。

语法

copy(1)               set& operator= (const set& x);                             //until C++ 11
copy (1)  set& operator= (const set& x);                //since C++ 11
move (2)  set& operator= (set&& x);                                   //since C++ 11
initializer list (3)  set& operator= (initializer_list<value_type> il);      //since C++ 11
copy(1):-将x中的所有元素复制到集合容器中。
move(2):-移动x的内容到集合容器中。
initializer_list(3):-将il的元素复制到集合容器中。

参数

x : 类型相同的 Set 对象。
il : 初始化列表对象。

返回值

此指针。

复杂度

副本分配: 大小为线性。
移动分配: 当前容器大小为线性。
启动器列表分配: 大小最大为对数。

迭代器有效性

与此集合相关的所有引用,迭代器和指针均无效。

数据竞争

访问所有复制的元素。
移动分配修改x。
set容器及其所有元素均为

异常安全性

如果引发异常,则容器处于有效状态。

示例1

让我们看一下将一个集合的内容复制到另一个集合的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<int> s1 = {10,20,30};
   cout << "Set s1 contains following elements" << endl;
    for (auto it = s1.begin(); it != s1.end(); ++it)
      cout << *it << endl;
      
    set<int> s2 = s1;  
    cout<<"\nAfter Copying the elements from s1 to s2... \n";  
    
    cout << "\nSet s2 contains following elements" << endl;
    for (auto it = s2.begin(); it != s2.end(); ++it)
      cout << *it<< endl; 
   return 0;
}
输出:
Set s1 contains following elements
10
20
30
After copying the elements from s1 to s2... 
Set s2 contains following elements
10
20
30
在上面的示例中,operator =用于将一个集合s1的内容复制到另一个集合s2、

示例2

让我们看一下将一组元素移动到另一组的简单示例:
#include <iostream> 
#include <set>
using namespace std;
int main(void) {
   
   set<char> s1 = {'a','e','i','o','u'};
      cout << "Set m1 contains following elements" << endl;
    for (auto it = s1.begin(); it != s1.end(); ++it)
      cout << *it << ", ";
      
    set<char> s2 = move(s1); 
    cout<<"\n\nAfter moving the elements from s1 to s2... \n";  
    
    cout << "\nSet s2 contains following elements" << endl;
    for (auto it = s2.begin(); it != s2.end(); ++it)
      cout << *it << ", ";
   return 0;
}
输出:
Set m1 contains following elements
a, e, i, o, u, 
After moving the elements from s1 to s2?
Set s2 contains following elements
a, e, i, o, u,
在上面的示例中,operator =用于将一个集合s1的内容移动到另一集合s2、

示例3

让我们看一下一个简单的示例,将内容从初始化列表复制到 Set :
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<int> s;
   s = {100, 200, 300, 400, 500};  //initializer list
   cout << "Set contains the following elements" << endl;
   for (auto it = s.begin(); it != s.end(); ++it)
      cout << *it << endl;
   return 0;
}
输出:
Set contains the following elements
100
200
300
400
500
在上面的示例中,operator =用于复制初始化程序列表中的内容以 Set m。

示例4

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int  main () 
{ 
  int  values []  =  {  5 ,  2 ,  4 ,  1 ,  0 ,  0 ,  9  }; 
  set < int >  c1 ( values ,  values  +  7 ); 
  set < int >  c2 ;
  c2  =  c1 ; 
  c1  =  set < int > ();
  cout<<  "Size Of c1:"  <<  c1 . size ()  << endl ; 
  cout<< "Size Of c2:"  <<  c2 . size ()  << endl ; 
}
输出:
Size Of c1:0
Size Of c2:6
在上面的示例中,有两组c1和c2、 c1有7个元素,c2为空,但是将c1分配给c2后,c1的大小变为0,c2的大小变为7、

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4