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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。