C++ Set operator=

C++ Set operator=

以下是set中operator=的三个用法:

  1. Operator =用于通过替换其旧内容(或复制内容)将新内容赋给set容器,并在必要时修改大小。
  2. Operator =用于将一个set容器的内容移动到另一个容器中,并在必要时修改大小。
  3. 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 中的所有元素拷贝到 set 容器中。

move (2) :将 x 的内容移动到 set 容器中。

initializer_list (3) :将 il 的元素拷贝到 set 容器中。

参数

x :与之类型相同的 set 对象。

il :一个初始化列表对象。

返回值

this 指针。

复杂度

拷贝赋值 :与大小成线性关系。

移动赋值 :与当前容器大小成线性关系。

初始化列表赋值 :与大小的对数关系。

迭代器有效性

与此 set 有关的所有引用、迭代器和指针都无效。

数据竞争

所有拷贝的元素都会被访问。

移动赋值会修改 x。

set 容器和其中的所有元素都会被修改。

异常安全性

如果抛出异常,容器仍处于有效状态。

示例1

让我们看一个简单的示例,将一个 set 的内容拷贝到另一个 set 中:

#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,

在上面的示例中,使用操作符 = 将一个集合 s1 的内容移动到另一个集合 s2。

示例3

让我们看一个简单的示例,将初始化列表的内容复制到集合中:

#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 = 将初始化列表中的内容复制到集合 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。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程