📜  C++ STL中的multiset ::运算符=

📅  最后修改于: 2021-05-30 07:03:16             🧑  作者: Mango

多重集是类似于set的一种关联容器,不同之处在于多个元素可以具有相同的值。

multiset ::运算符=

该运算符用于通过替换现有内容将新内容分配给容器。
它还根据新内容修改大小。

句法 :

multisetname1 = (multisetname2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as 
parameter to the container written on left side of the operator.
设置::运算符=

该运算符用于通过替换现有内容将新内容分配给容器。
它还根据新内容修改大小。

句法 :

setname1 = (setname2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as 
parameter to the container written on left side of the operator.

例子:

Input  :  mymultiset1 = 1, 2, 3
          mymultiset2 = 3, 2, 1, 4
          mymultiset1 = mymultiset2;
Output :  mymultiset1 = 3, 2, 1, 4

Input  :  mymultiset1 = 2, 6, 1, 5
          mymultiset2 = 3, 2
          mymultiset1 = mymultiset2;
Output :  mymultiset1 = 3, 2
Input  :  myset1 = 1, 2, 3
          myset2 = 3, 2, 1, 4
          myset1 = myset2;
Output :  myset1 = 3, 2, 1, 4

Input  :  myset1 = 2, 6, 1, 5
          myset2 = 3, 2
          myset1 = myset2;
Output :  myset1 = 3, 2

错误和异常

1.如果容器属于不同类型,则会引发错误。
2.否则,它有一个基本的无异常抛出保证。

// INTEGER MULISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include 
#include 
using namespace std;
   
int main()
{
    multiset mymultiset1{ 1, 7, 4, 9, 0};
    multiset mymultiset2{ 3, 4 };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

mymultilist1 = 3 4
// CHARACTER MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include 
#include 
using namespace std;
   
int main()
{
    multiset mymultiset1{ 'a', 'b', 'c'};
    multiset mymultiset2{ 'x', 'y' };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

mymultilist1 = x y
// STRING MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include 
#include 
#include
using namespace std;
   
int main()
{
    multiset mymultiset1{ "This","is","a","computer science portal"};
    multiset mymultiset2{ "GeeksForGeeks" };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

mymultilist1 = GeeksForGeeks

时间复杂度: O(n)

// CPP program to illustrate
// Implementation of = operator
#include 
#include 
#include
using namespace std;
   
int main()
{
    set myset1{ "This","is","a","computer science portal"};
    set myset2{ "GeeksForGeeks" };
    myset1 = myset2;
    cout << "myset1 = ";
    for (auto it = myset1.begin();
              it != myset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

mylist1 = GeeksForGeeks

时间复杂度: O(n)

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”