C++ STL multimap, constructor or ctor program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

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:

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

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

 

// C++ STL multimap, constructor or ctor

// notice the duplicate key and data element

#include <map>

#include <iostream>

using namespace std;

 

int main(void)

{

// simplify the pair<int, int> to Int_Pair

typedef pair<int, int> Int_Pair;

 

// iterators

multimap<int, int>::iterator mmp0Iter, mmp1Iter, mmp3Iter, mmp4Iter, mmp5Iter, mmp6Iter;

multimap<int, int, greater<int> >::iterator mmp2Iter;

 

// create an empty multimap mmp0 of key type integer

multimap <int, int> mmp0;

 

// do some operations

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

multimap<int, int, less<int> > mmp1;

mmp1.insert(Int_Pair(2, 2));

mmp1.insert(Int_Pair(2, 21));

mmp1.insert(Int_Pair(1, 5));

mmp1.insert(Int_Pair(3, 12));

mmp1.insert(Int_Pair(5, 32));

mmp1.insert(Int_Pair(4, 21));

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

multimap <int, int, greater<int> > mmp2;

mmp2.insert(Int_Pair(1, 11));

mmp2.insert(Int_Pair(2, 10));

mmp2.insert(Int_Pair(2, 11));

mmp2.insert(Int_Pair(3, 12));

// create a multimap mmp3 with the allocator of multimap mmp1

multimap <int, int>::allocator_type mmp1_Alloc;

mmp1_Alloc = mmp1.get_allocator();

multimap <int, int> mmp3(less<int>(), mmp1_Alloc);

mmp3.insert(Int_Pair(3, 12));

mmp3.insert(Int_Pair(1, 21));

// multimap mmp4, a copy of multimap mmp1

multimap <int, int> mmp4(mmp1);

// create a multimap mmp5 by copying the range mmp1[_First, _Last)

multimap <int, int>::const_iterator mmp1_PIter, mmp1_QIter;

mmp1_PIter = mmp1.begin();

mmp1_QIter = mmp1.begin();

mmp1_QIter++;

mmp1_QIter++;

multimap <int, int> mmp5(mmp1_PIter, mmp1_QIter);

// create a multimap mmp6 by copying the range mmp4[_First, _Last) and with the allocator of multimap mmp2

multimap <int, int>::allocator_type mmp2_Alloc;

mmp2_Alloc = mmp2.get_allocator();

multimap <int, int> mmp6(mmp4.begin(), ++mmp4.begin(), less<int>(), mmp2_Alloc);

// more operations

cout<<"Operation: multimap <int, int> mmp0;"<<endl;

cout<<"mmp0 multimap data: ";

for(mmp0Iter = mmp0.begin(); mmp0Iter != mmp0.end(); mmp0Iter++)

cout<<" "<<mmp0Iter->second;

cout<<endl;

cout<<"\nOperation1: multimap<int, int, less<int> > mmp1;"<<endl;

cout<<"Operation2: mmp1.insert(Int_Pair(2, 2))..."<<endl;

cout<<"mmp1 multimap data: ";

for(mmp1Iter = mmp1.begin(); mmp1Iter != mmp1.end(); mmp1Iter++)

cout<<" "<<mmp1Iter->second;

cout<<endl;

cout<<"\nOperation1: multimap <int, int, greater<int> > mmp2;"<<endl;

cout<<"Operation2: mmp2.insert(Int_Pair(1, 11))..."<<endl;

cout<<"mmp2 multimap data: ";

for(mmp2Iter = mmp2.begin(); mmp2Iter != mmp2.end(); mmp2Iter++)

cout<<" "<<mmp2Iter->second;

cout<<endl;

cout<<"\nOperation1: multimap <int, int> mmp3(less<int>(), mmp1_Alloc);"<<endl;

cout<<"Operation2: mmp3.insert(Int_Pair(3, 12))..."<<endl;

cout<<"mmp3 multimap data: ";

for(mmp3Iter = mmp3.begin(); mmp3Iter != mmp3.end(); mmp3Iter++)

cout<<" "<<mmp3Iter->second;

cout<<endl;

cout<<"\nOperation: multimap <int, int> mmp4(mmp1)"<<endl;

cout<<"mmp4 multimap data: ";

for(mmp4Iter = mmp4.begin(); mmp4Iter != mmp4.end(); mmp4Iter++)

cout<<" "<<mmp4Iter->second;

cout << endl;

cout<<"\nOperation: multimap <int, int> mmp5(mmp1_PIter, mmp1_QIter)"<<endl;

cout<<"mmp5 multimap data: ";

for(mmp5Iter = mmp5.begin(); mmp5Iter != mmp5.end(); mmp5Iter++)

cout<<" "<<mmp5Iter->second;

cout<<endl;

cout<<"\nOperation: multimap <int, int> mmp6(mmp4.begin(), ++mmp4.begin(), less<int>(), mmp2_Alloc)"<<endl;

cout<<"mmp6 multimap data: ";

for(mmp6Iter = mmp6.begin(); mmp6Iter != mmp6.end(); mmp6Iter++)

cout<<" "<<mmp6Iter->second;

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: multimap <int, int> mmp0;

mmp0 multimap data:

Operation1: multimap<int, int, less<int> > mmp1;

Operation2: mmp1.insert(Int_Pair(2, 2))...

mmp1 multimap data: 5 2 21 12 21 32

Operation1: multimap <int, int, greater<int> > mmp2;

Operation2: mmp2.insert(Int_Pair(1, 11))...

mmp2 multimap data: 12 10 11 11

Operation1: multimap <int, int> mmp3(less<int>(), mmp1_Alloc);

Operation2: mmp3.insert(Int_Pair(3, 12))...

mmp3 multimap data: 21 12

Operation: multimap <int, int> mmp4(mmp1)

mmp4 multimap data: 5 2 21 12 21 32

Operation: multimap <int, int> mmp5(mmp1_PIter, mmp1_QIter)

mmp5 multimap data: 5 2

Operation: multimap <int, int> mmp6(mmp4.begin(), ++mmp4.begin(), less<int>(), mmp2_Alloc)

mmp6 multimap data: 5

Press any key to continue . . .

 

 

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