當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。