C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset swap()C++ Multiset swap() function is used to swap (or exchange) the contents of two multisets but both the multisets must be of same type although sizes may differ. Syntaxvoid swap (multiset& x); Parameterx: multiset container to exchange the contents with. Return valueNone ComplexityConstant. Iterator validityAll references, iterators and pointers referring to elements in both multiset containers remain valid, but now are referring to elements in the other multiset container, and iterate on it. Data RacesBoth the container and x are modified. Exception SafetyNo effect on container if exception is thrown. Example 1Let's see the simple example to swap the element of one multiset to another: #include <iostream> #include <set> using namespace std; int main(void) { multiset<int> m1 = {1,2,3,4,5,3}; multiset<int> m2; m2.swap(m1); cout << "Multiset m2 contains following elements" << endl; for (auto it = m2.begin(); it != m2.end(); ++it) cout << *it<< endl; return 0; } Output: Multiset m2 contains following elements 1 2 3 3 4 5 In the above example, multiset m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2. Example 2Let's see a simple example to exchange the contents of two multisets: #include <iostream> #include <set> using namespace std; int main () { int myints[]={19,72,4,36,20,20}; multiset<int> first (myints,myints+3); // 4,19,72 multiset<int> second (myints+3,myints+6); // 20,20,36 first.swap(second); cout << "first contains:"; for (multiset<int>::iterator it=first.begin(); it!=first.end(); ++it) cout << ' ' << *it; cout << '\n'; cout << "second contains:"; for (multiset<int>::iterator it=second.begin(); it!=second.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; } Output: first contains: 20 20 36 second contains: 4 19 72 Example 3Let's see a simple example to swap the contents of two multisets: #include<iostream> #include<set> using namespace std; int main() { // Take any two multisets multiset<char> multiset1, multiset2; multiset1 = {'a','b','c','d','c'}; multiset2 = {'x','y','z','y'}; // Swap elements of multisets swap(multiset1, multiset2); // Print the elements of multisets cout << "multiset1:\n"; for (auto it = multiset1.begin(); it != multiset1.end(); it++) cout << "\t" << *it<< '\n'; cout << "multiset2:\n"; for (auto it = multiset2.begin(); it != multiset2.end(); it++) cout << "\t" << *it<< '\n'; return 0; } Output: multiset1: x y y z multiset2: a b c c d In the above example, another form of swap() function is used to swap the contents of two multisets. Example 4Let's see a simple example: #include <set> #include <iostream> int main( ) { using namespace std; multiset <int> s1, s2, s3; multiset <int>::iterator s1_Iter; s1.insert( 10 ); s1.insert( 20 ); s1.insert( 10 ); s2.insert( 100 ); s2.insert( 200 ); s3.insert( 200 ); cout << "The original multiset s1 is:"; for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ ) cout << " " << *s1_Iter; cout << "." << endl; // This is the member function version of swap s1.swap( s2 ); cout << "After swapping with s2, multiset s1 is:"; for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ ) cout << " " << *s1_Iter; cout << "." << endl; // This is the specialized template version of swap swap( s1, s3 ); cout << "After swapping with s3, multiset s1 is:"; for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ ) cout << " " << *s1_Iter; cout << "." << endl; } Output: The original multiset s1 is: 10 10 20. After swapping with s2, multiset s1 is: 100 200. After swapping with s3, multiset s1 is: 200.
Next TopicC++ multiset
|