C++ STL map, constructors 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++ map to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other map in C++ programming
To show: How to use the C++ map, constructor to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other map in C++ programming
// C++ STL map, constructor
#include <map>
#include <iostream>
using namespace std;
int main(void)
{
// simplify pair<int, int> to Int_Pair
typedef pair<int, int> Int_Pair;
// iterators
map<int, int>::iterator mp0_Iter, mp1_Iter, mp3_Iter, mp4_Iter, mp5_Iter, mp6_Iter;
map<int, int, greater<int> >::iterator mp2_Iter;
// create an empty map mp0 of key type integer
map <int, int> mp0;
// create an empty map mp1 with the key comparison function of less than, then insert 6 elements
map <int, int, less<int> > mp1;
mp1.insert(Int_Pair(1, 13));
mp1.insert(Int_Pair(3, 23));
mp1.insert(Int_Pair(3, 31));
mp1.insert(Int_Pair(2, 23));
mp1.insert(Int_Pair(6, 15));
mp1.insert(Int_Pair(9, 25));
// create an empty map mp2 with the key comparison function of greater than, then insert 3 elements
map <int, int, greater<int> > mp2;
mp2.insert(Int_Pair(3, 12));
mp2.insert(Int_Pair(1, 31));
mp2.insert(Int_Pair(2, 21));
// create a map mp3 with the allocator of map mp1
map <int, int>::allocator_type mp1_Alloc;
mp1_Alloc = mp1.get_allocator();
map <int, int> mp3(less<int>(), mp1_Alloc);
mp3.insert(Int_Pair(1, 10));
mp3.insert(Int_Pair(2, 12));
// create a copy, map mp4, of map mp1
map <int, int> mp4(mp1);
// create a map mp5 by copying the range mp1[_First, _Last)
map <int, int>::const_iterator mp1_PIter, mp1_QIter;
mp1_PIter = mp1.begin();
mp1_QIter = mp1.begin();
mp1_QIter++;
mp1_QIter++;
map <int, int> mp5(mp1_PIter, mp1_QIter);
// create a map mp6 by copying the range mp4[_First, _Last) and with the allocator of map mp2
map <int, int>::allocator_type mp2_Alloc;
mp2_Alloc = mp2.get_allocator();
map <int, int> mp6(mp4.begin(), ++mp4.begin(), less<int>(), mp2_Alloc);
// more operations
cout<<"Operation: map <int, int> mp0;"<<endl;
cout<<"mp0 map data: ";
for(mp0_Iter = mp0.begin(); mp0_Iter != mp0.end(); mp0_Iter++)
cout<<" "<<mp0_Iter->second;
cout<<endl;
cout<<"\nOperation1: map <int, int, less<int> > mp1;"<<endl;
cout<<"Operation2: mp1.insert(Int_Pair(1, 13))..."<<endl;
cout<<"mp1 map data: ";
for(mp1_Iter = mp1.begin(); mp1_Iter != mp1.end(); mp1_Iter++)
cout<<" "<<mp1_Iter->second;
cout<<endl;
cout<<"\nOperation1: map <int, int, greater<int> > mp2;"<<endl;
cout<<"Operation2: mp2.insert(Int_Pair(3, 12))..."<<endl;
cout<<"mp2 map data: ";
for(mp2_Iter = mp2.begin(); mp2_Iter != mp2.end(); mp2_Iter++)
cout<<" "<<mp2_Iter->second;
cout<<endl;
cout<<"\nOperation1: map <int, int> mp3(less<int>(), mp1_Alloc);"<<endl;
cout<<"Operation2: mp3.insert(Int_Pair(1, 10))..."<<endl;
cout<<"mp3 map data: ";
for(mp3_Iter = mp3.begin(); mp3_Iter != mp3.end(); mp3_Iter++)
cout<<" "<<mp3_Iter->second;
cout<<endl;
cout<<"\nOperation: map <int, int> mp4(mp1);"<<endl;
cout<<"mp4 map data: ";
for(mp4_Iter = mp4.begin(); mp4_Iter != mp4.end(); mp4_Iter++)
cout<<" "<<mp4_Iter->second;
cout<<endl;
cout<<"\nOperation: map <int, int> mp5(mp1_PIter, mp1_QIter);"<<endl;
cout<<"mp5 map data: ";
for(mp5_Iter = mp5.begin(); mp5_Iter != mp5.end(); mp5_Iter++)
cout<<" "<<mp5_Iter->second;
cout<<endl;
cout<<"\nOperation: map <int, int> mp6(mp4.begin(), ++mp4.begin(), less<int>(), mp2_Alloc);"<<endl;
cout<<"mp6 map data: ";
for(mp6_Iter = mp6.begin(); mp6_Iter != mp6.end(); mp6_Iter++)
cout<<" "<<mp6_Iter->second;
cout<<endl;
return 0;
}
Output examples:
Operation: map <int, int> mp0;
mp0 map data:
Operation1: map <int, int, less<int> > mp1;
Operation2: mp1.insert(Int_Pair(1, 13))...
mp1 map data: 13 23 23 15 25
Operation1: map <int, int, greater<int> > mp2;
Operation2: mp2.insert(Int_Pair(3, 12))...
mp2 map data: 12 21 31
Operation1: map <int, int> mp3(less<int>(), mp1_Alloc);
Operation2: mp3.insert(Int_Pair(1, 10))...
mp3 map data: 10 12
Operation: map <int, int> mp4(mp1);
mp4 map data: 13 23 23 15 25
Operation: map <int, int> mp5(mp1_PIter, mp1_QIter);
mp5 map data: 13 23
Operation: map <int, int> mp6(mp4.begin(), ++mp4.begin(), less<int>(), mp2_Alloc);
mp6 map data: 13
Press any key to continue . . .