多重集是类似于set的一种关联容器,不同之处在于多个元素可以具有相同的值。
multiset :: swap()
此函数用于交换两个多集的内容,但是集的类型必须相同,尽管大小可能会有所不同。
句法 :
multisetname1.swap(multisetname2)
Parameters :
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
using namespace std;
int main()
{
// Take any two multisets
multiset multiset1{ 1, 2, 3, 4 };
multiset 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
using namespace std;
int main()
{
// Take any two multisets
multiset multiset1{ "Geeksforgeeks" };
multiset 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
using namespace std;
int main()
{
// Take any two multisets
multiset multiset1{ 'A', 'B', 'C' };
multiset 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
时间复杂度:恒定
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。