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

C++ multiset operator=

C++ multise operator=

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

语法

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

参数

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

返回值

此指针。

复杂度

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

迭代器有效性

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

数据竞争

所有复制的元素都可以访问。
移动分配修改x。
multiset 容器

异常安全性

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

示例1

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

示例2

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

示例3

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

示例4

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int  main () 
{ 
  int  values []  =  {  5 ,  2 ,  4 ,  1 ,  0 ,  0 ,  9  }; 
  multiset < int >  c1 ( values ,  values  +  7 ); 
  multiset < int >  c2 ;
  c2  =  c1 ; 
  c1  =  multiset < int > ();
  cout<<  "Size Of c1:"  <<  c1 . size ()  << endl ; 
  cout<< "Size Of c2:"  <<  c2 . size ()  << endl ; 
}
输出:
Size Of c1:0
Size Of c2:7
在上面的示例中,有两个 multiset c1和c2、 c1有7个元素,c2为空,但是在将c1分配给c2之后,c1的大小变为0,c2的大小变为7、
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4