C++ STL multiset, constructors program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ multiset to construct a multiset that is empty or that is a copy of all or part of some other multiset in C++ programming

To show: How to use the C++ multiset, constructor to construct a multiset that is empty or that is a copy of all or part of some other multiset in C++ programming




// C++ STL multiset, constructors

#include <set>

#include <iostream>

using namespace std;


int main(void)


// iterators

multiset <int>::iterator mst0_Iter, mst1_Iter, mst2_Iter, mst3_Iter;

multiset <int>::iterator mst4_Iter, mst5_Iter, mst6_Iter;


// create an empty multiset mst0 of key type integer, a container

multiset <int> mst0;

// create an empty multiset mst1 with the key comparison function of less than, then insert 6 elements

multiset <int, less<int> > mst1;








// create an empty multiset mst2 with the key comparison function of greater than, then insert 4 elements

multiset <int, greater<int> > mst2;






// create a multiset mst3 with the allocator of multiset mst1

multiset <int>::allocator_type mst1_Alloc;

mst1_Alloc = mst1.get_allocator();

multiset <int> mst3(less<int>(), mst1_Alloc);




// multiset mst4, a copy of multiset mst1

multiset <int> mst4(mst1);

// create a multiset mst5 by copying the range mst1[_First, _Last)

multiset <int>::const_iterator mst1_PIter, mst1_QIter;

mst1_PIter = mst1.begin();

mst1_QIter = mst1.begin();



multiset <int> mst5(mst1_PIter, mst1_QIter);

// create a multiset mst6 by copying the range mst4[_First, _Last) and with the allocator of multiset mst2

multiset <int>::allocator_type mst2_Alloc;

mst2_Alloc = mst2.get_allocator();

multiset <int> mst6(mst4.begin(), ++mst4.begin(), less<int>(), mst2_Alloc);

// more operations

cout<<"Operation: multiset <int> mst0;"<<endl;

cout<<"mst0 multiset data: ";

for(mst0_Iter = mst0.begin(); mst0_Iter != mst0.end(); mst0_Iter++)

cout<<" " <<*mst0_Iter;


cout<<"\nOperation: multiset <int, less<int> > mst1;"<<endl;

cout<<"Operation: mst1.insert(41)..."<<endl;

cout<<"mst1 multiset data: ";

for(mst1_Iter = mst1.begin(); mst1_Iter != mst1.end(); mst1_Iter++)

cout<<" "<<*mst1_Iter;


cout<<"\nOperation: multiset <int, greater<int> > mst2;"<<endl;

cout<<"Operation: mst2.insert(10)..."<<endl;

cout<<"mst2 multiset data: "<<*mst2.begin()<<" "<<*++mst2.begin()<<endl;

cout<<"\nOperation: multiset <int> mst3(less<int>(), mst1_Alloc);"<<endl;

cout<<"Operation: mst3.insert(7)..."<<endl;

cout<<"mst3 multiset data: ";

for(mst3_Iter = mst3.begin(); mst3_Iter != mst3.end(); mst3_Iter++)

cout<<" "<<*mst3_Iter;


cout<<"\nOperation: multiset <int> mst4(mst1);"<<endl;

cout<<"mst4 multiset data: ";

for(mst4_Iter = mst4.begin(); mst4_Iter != mst4.end(); mst4_Iter++)

cout<<" "<<*mst4_Iter;


cout<<"\nOperation: multiset <int> mst5(mst1_PIter, mst1_QIter);"<<endl;

cout<<"mst5 multiset data: ";

for(mst5_Iter = mst5.begin(); mst5_Iter != mst5.end(); mst5_Iter++)

cout<<" "<<*mst5_Iter;


cout<<"\nOperation: multiset <int> mst6(mst4.begin(), ++mst4.begin(), less<int>(), mst2_Alloc);"<<endl;

cout<<"mst6 multiset data: ";

for(mst6_Iter = mst6.begin(); mst6_Iter != mst6.end(); mst6_Iter++)

cout<<" "<<*mst6_Iter;


return 0;



Output examples:


Operation: multiset <int> mst0;

mst0 multiset data:

Operation: multiset <int, less<int> > mst1;

Operation: mst1.insert(41)...

mst1 multiset data: 10 15 15 30 35 41

Operation: multiset <int, greater<int> > mst2;

Operation: mst2.insert(10)...

mst2 multiset data: 21 21

Operation: multiset <int> mst3(less<int>(), mst1_Alloc);

Operation: mst3.insert(7)...

mst3 multiset data: 7 9 12

Operation: multiset <int> mst4(mst1);

mst4 multiset data: 10 15 15 30 35 41

Operation: multiset <int> mst5(mst1_PIter, mst1_QIter);

mst5 multiset data: 10 15

Operation: multiset <int> mst6(mst4.begin(), ++mst4.begin(), less<int>(), mst2_Alloc);

mst6 multiset data: 10

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index