当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ multiset::operator=用法及代码示例


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

多集::运算符=

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

用法:

multisetname1 = (multisetname2)
参数:
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)
参数:
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 <iostream>
#include <set>
using namespace std;
   
int main()
{
    multiset<int> mymultiset1{ 1, 7, 4, 9, 0};
    multiset<int> 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 <iostream>
#include <set>
using namespace std;
   
int main()
{
    multiset<char> mymultiset1{ 'a', 'b', 'c'};
    multiset<char> 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 <iostream>
#include <set>
#include<string>
using namespace std;
   
int main()
{
    multiset<string> mymultiset1{ "This","is","a","computer science portal"};
    multiset<string> mymultiset2{ "GeeksForGeeks" };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

mymultilist1 = GeeksForGeeks

时间复杂度:在)


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

输出:

mylist1 = GeeksForGeeks

时间复杂度:在)




相关用法


注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 multiset::operator= in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。