More program examples on C++ STL Container, compiled using VC++7.0 / .Net, win32 empty console mode application. Be careful with the source codes than span more than one line. Linux g++ compilation examples are given at the end of this Module. Source code is available in C++ STL Container source code.
|
|
29.1 map
|
||||||||||||
|
Operators |
Description |
|
operator!= |
Tests if the map or multimap object on the left side of the operator is not equal to the map or multimap object on the right side. |
|
operator< |
Tests if the map or multimap object on the left side of the operator is less than the map or multimap object on the right side. |
|
operator<= |
Tests if the map or multimap object on the left side of the operator is less than or equal to the map or multimap object on the right side. |
|
operator== |
Tests if the map or multimap object on the left side of the operator is equal to the map or multimap object on the right side. |
|
operator> |
Tests if the map or multimap object on the left side of the operator is greater than the map or multimap object on the right side. |
|
operator>= |
Tests if the map or multimap object on the left side of the operator is greater than or equal to the map or multimap object on the right side. |
|
Table 29.2 |
|
|
Specialized template function |
Description |
|
swap() |
Exchanges the elements of two maps or multimaps. |
|
Table 29.3 |
|
|
Class |
Description |
|
value_compare |
Provides a function object that can compare the elements of a map by comparing the values of their keys to determine their relative order in the map. |
|
map |
Used for the storage and retrieval of data from a collection in which the each of the elements has a unique key with which the data is automatically ordered. |
|
multimap |
Used for the storage and retrieval of data from a collection in which the each of the elements has a key with which the data is automatically ordered and the keys do not need to have unique values. |
|
Table 29.4 |
|
|
Template Class Member |
Description |
|
allocator_type |
A type that represents the allocator class for the map object. |
|
const_iterator |
A type that provides a bidirectional iterator that can read a const element in the map. |
|
const_pointer |
A type that provides a pointer to a const element in a map. |
|
const_reference |
A type that provides a reference to a const element stored in a map for reading and performing const operations. |
|
const_reverse_iterator |
A type that provides a bidirectional iterator that can read any const element in the map. |
|
difference_type |
A signed integer type that can be used to represent the number of elements of a map in a range between elements pointed to by iterators. |
|
iterator |
A type that provides a bidirectional iterator that can read or modify any element in a map. |
|
key_compare |
A type that provides a function object that can compare two sort keys to determine the relative order of two elements in the map. |
|
key_type |
A type that describes the sort key object which constitutes each element of the map. |
|
mapped_type |
A type that represents the data type stored in a map. |
|
pointer |
A type that provides a pointer to a const element in a map. |
|
reference |
A type that provides a reference to an element stored in a map. |
|
reverse_iterator |
A type that provides a bidirectional iterator that can read or modify an element in a reversed map. |
|
size_type |
An unsigned integer type that can represent the number of elements in a map |
|
value_type |
A type that provides a function object that can compare two elements as sort keys to determine their relative order in the map. |
|
Table 29.5 |
|
|
Template class member function |
Description |
|
begin() |
Returns an iterator addressing the first element in the map. |
|
clear() |
Erases all the elements of a map. |
|
count() |
Returns the number of elements in a map whose key matches a parameter-specified key. |
|
empty() |
Tests if a map is empty. |
|
end() |
Returns an iterator that addresses the location succeeding the last element in a map. |
|
equal_range() |
Returns an iterator that addresses the location succeeding the last element in a map. |
|
erase() |
Removes an element or a range of elements in a map from specified positions |
|
find() |
Returns an iterator addressing the location of an element in a map that has a key equivalent to a specified key. |
|
get_allocator() |
Returns a copy of the allocator object used to construct the map. |
|
insert() |
Inserts an element or a range of elements into the map at a specified position. |
|
key_comp() |
Retrieves a copy of the comparison object used to order keys in a map. |
|
lower_bound() |
Returns an iterator to the first element in a map that with a key value that is equal to or greater than that of a specified key. |
|
map() |
map constructor, constructs 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. |
|
max_size() |
Returns the maximum length of the map. |
|
rbegin() |
Returns an iterator addressing the first element in a reversed map. |
|
rend() |
Returns an iterator that addresses the location succeeding the last element in a reversed map. |
|
size() |
Specifies a new size for a map. |
|
swap() |
Exchanges the elements of two maps. |
|
upper_bound() |
Returns an iterator to the first element in a map that with a key value that is greater than that of a specified key. |
|
value_comp() |
Retrieves a copy of the comparison object used to order element values in a map. |
|
Table 29.6 |
|
|
Operator |
Description |
|
operator[ ] |
Inserts an element into a map with a specified key value. |
|
Table 29.7 |
|
The STL map class is used for the storage and retrieval of data from a collection in which the each element is a pair that has both a data value and a sort key.
The value of the key is unique and is used to order the data is automatically. The value of an element in a map, but not its associated key value, may be changed directly. Instead, key values associated with old elements must be deleted and new key values associated with new elements inserted.
template < class Key, class Type, class Traits = less<Key>, class Allocator = allocator<pair <const Key, Type> > >
|
Parameter |
Description |
|
Key |
The key data type to be stored in the map. |
|
Type |
The element data type to be stored in the map. |
|
Traits |
The type that provides a function object that can compare two element values as sort keys to determine their relative order in the map. This argument is optional and the binary predicate less<Key> is the default value. |
|
Allocator |
The type that represents the stored allocator object that encapsulates details about the map's allocation and de-allocation of memory. This argument is optional and the default value is allocator<pair <const Key, Type> >. |
|
Table 29.8 |
|
The STL map class is:
An associative container, which a variable size container that supports the efficient retrieval of element values based on an associated key value.
Reversible, because it provides bidirectional iterators to access its elements.
Sorted, because its elements are ordered by key values within the container in accordance with a specified comparison function.
Unique in the sense that each of its elements must have a unique key.
A pair associative container, because its element data values are distinct from its key values.
A template class, because the functionality it provides is generic and so independent of the specific type of data contained as elements or keys. The data types to be used for elements and keys are, instead, specified as parameters in the class template along with the comparison function and allocator.
|
// map, constructor, compiled with VC++ 7.0 or .Net
#include <map>
#include <iostream>
using namespace std;
int main( )
{
typedef pair<int, int> Int_Pair;
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);
// --------------------------------------------------------
cout<<"Operation: map <int, int> mp0\n";
cout<<"mp0 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\n";
cout<<"Operation2: mp1.insert(Int_Pair(1, 13))...\n";
cout<<"mp1 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\n";
cout<<"Operation2: mp2.insert(Int_Pair(3, 12))...\n";
cout<<"mp2 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)\n";
cout<<"Operation2: mp3.insert(Int_Pair(1, 10))...\n";
cout<<"mp3 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)\n";
cout<<"mp4 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)\n";
cout<<"mp5 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(), \n++mp4.begin(), less<int>(), mp2_Alloc);\n";
cout<<"mp6 data: ";
for(mp6_Iter = mp6.begin(); mp6_Iter != mp6.end(); mp6_Iter++)
cout<<" "<<mp6_Iter->second;
cout<<endl;
return 0;
}
Output:
------------------------------------------------------------------------------------------------------

-----------------------------------------------------End of map------------------------------------------------
---www.tenouk.com---
A multimap is the same as a map except that duplicates are allowed. Thus, a multimap may contain multiple elements that have the same key. A multimap can also be used as dictionary. It can be depicted as follows:

The iterator provided by the map class is a bidirectional iterator, but the class member functions insert() and multimap() have versions that take as template parameters a weaker input iterator, whose functionality requirements are more minimal than those guaranteed by the class of bidirectional iterators.
The multimap orders the sequence it controls by calling a stored function object of type key_compare. This stored object is a comparison function that may be accessed by calling the member function key_comp().
The (key, value) pairs are stored in a multimap as objects of type pair. The pair class requires the header <utility>, which is automatically included by <map>.
|
Typedef |
Description |
|
allocator_type |
A type that represents the allocator class for the multimap object. |
|
const_iterator |
A type that provides a bidirectional iterator that can read a const element in the multimap. |
|
const_pointer |
A type that provides a pointer to a const element in a multimap. |
|
const_reference |
A type that provides a reference to a const element stored in a multimap for reading and performing const operations. |
|
const_reverse_iterator |
A type that provides a bidirectional iterator that can read any const element in the multimap. |
|
difference_type |
A signed integer type that can be used to represent the number of elements of a multimap in a range between elements pointed to by iterators. |
|
iterator |
A type that provides the difference between two iterators those refer to elements within the same multimap. |
|
key_compare |
A type that provides a function object that can compare two sort keys to determine the relative order of two elements in the multimap. |
|
key_type |
A type that describes the sort key object that constitutes each element of the multimap. |
|
mapped_type |
A type that represents the data type stored in a multimap. |
|
pointer |
A type that provides a pointer to a const element in a multimap. |
|
reference |
A type that provides a reference to an element stored in a multimap. |
|
reverse_iterator |
A type that provides a bidirectional iterator that can read or modify an element in a reversed multimap. |
|
size_type |
An unsigned integer type that provides a pointer to a const element in a multimap |
|
value_type |
A type that provides a function object that can compare two elements as sort keys to determine their relative order in the multimap |
|
Table 29.9 |
|
|
Member function |
Description |
|
begin() |
Returns an iterator addressing the first element in the multimap. |
|
clear() |
Erases all the elements of a multimap. |
|
count() |
Returns the number of elements in a multimap whose key matches a parameter-specified key. |
|
empty() |
Tests if a multimap is empty. |
|
end() |
Returns an iterator that add |