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


C++ multiset::swap()用法及代碼示例


多重集是類似於set的一種關聯容器,不同之處在於多個元素可以具有相同的值。

multiset::swap()

此函數用於交換兩個多集的內容,但是集的類型必須相同,盡管大小可能會有所不同。

用法:

multisetname1.swap(multisetname2)
參數:
The name of the multiset with which
the contents have to be swapped.

Result:
All the elements of the 2 multiset are swapped.

例子:

Input :multiset1 = {1, 2, 3, 4}
         multiset2 = {5, 6, 7, 8}
         multiset1.swap(multiset2);
Output:multiset1 = {5, 6, 7, 8}
         multiset2 = {1, 2, 3, 4}

Input :multiset1 = {'a', 'b', 'c', 'd'}
         multiset2 = {'w', 'x', 'y', 'z'}
         multiset1.swap(multiset2);
Output:multiset1 = {'w', 'x', 'y', 'z'}
         multiset2 = {'a', 'b', 'c', 'd'}

// INTEGER MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of swap() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any two multisets
    multiset<int> multiset1{ 1, 2, 3, 4 };
    multiset<int> multiset2{ 5, 6, 7, 8 };
  
    // Swap elements of multisets
    multiset1.swap(multiset2);
  
    // Print the first multi set
    cout << "multiset1 = ";
    for (auto it = multiset1.begin();
         it != multiset1.end(); ++it)
        cout << ' ' << *it;
  
    // Print the second multiset
    cout << endl
         << "multiset2 = ";
    for (auto it = multiset2.begin();
         it != multiset2.end(); ++it)
        cout << ' ' << *it;
  
    return 0;
}

輸出:



multiset1 =  5 6 7 8
multiset2 =  1 2 3 4

// STRING MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of swap() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any two multisets
    multiset<string> multiset1{ "Geeksforgeeks" };
  
    multiset<string> multiset2{ "Computer scince", "Portal" };
  
    // Swap elements of multisets
    multiset1.swap(multiset2);
  
    // Print the first multi set
    cout << "multiset1 = ";
    for (auto it = multiset1.begin();
         it != multiset1.end(); ++it)
        cout << ' ' << *it;
  
    // Print the second multiset
    cout << endl
         << "multiset2 = ";
    for (auto it = multiset2.begin();
         it != multiset2.end(); ++it)
        cout << '

輸出:

multiset1 =  Computer science portal
multiset2 =  Geeksforgeeks

// CHARACTER MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of swap() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any two multisets
    multiset<char> multiset1{ 'A', 'B', 'C' };
    multiset<char> multiset2{ 'G', 'H', 'I' };
  
    // Swap elements of multisets
    multiset1.swap(multiset2);
  
    // Print the first multi set
    cout << "multiset1 = ";
    for (auto it = multiset1.begin();
         it != multiset1.end(); ++it)
        cout << ' ' << *it;
  
    // Print the second multiset
    cout << endl
         << "multiset2 = ";
    for (auto it = multiset2.begin();
         it != multiset2.end(); ++it)
        cout << ' ' << *it;
  
    re

輸出:

multiset1 =  G H I
multiset2 =  A B C

時間複雜度:持續的




相關用法


注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 multiset::swap() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。