|
explicit map ( const Compare& comp = Compare(),
const Allocator& = Allocator() );
template <class InputIterator>
map ( InputIterator first, InputIterator last,
const Compare& comp = Compare(), const Allocator& = Allocator() );
map ( const map<Key,T,Compare,Allocator>& x );
Construct map
Constructs a map container object, initializing its contents depending on the constructor version used:
- explicit map ( const Compare& comp = Compare(), Allocator& = Allocator() );
- Default constructor: constructs an empty map object, with no content and a size of zero.
- template <class InputIterator> map ( InputIterator first, InputIterator last, const Compare& comp= Compare(), const Allocator& = Allocator() );
- Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container object.
- map ( const map<Key,T,Compare,Allocator>& x );
- Copy constructor: The object is initialized to have the same contents and properties as the x map object.
Parameters
- first, last
- Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template type can be any type of input iterator.
- x
- Another map object with the same class template parameters (Key, T, Compare and Allocator).
- comp
- Comparison object to be used for the strict weak ordering.
Compare is the third class template parameter (see class description).
- unnamed Allocator parameter
- Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
// constructing maps
#include <iostream>
#include <map>
using namespace std;
bool fncomp (char lhs, char rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};
int main ()
{
map<char,int> first;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
map<char,int> second (first.begin(),first.end());
map<char,int> third (second);
map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp;
map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
return 0;
}
|
The code does not produce any output, but demonstrates some ways in which a map container can be constructed.
Complexity
For the default constructor, constant.
For the iterator constructor, linear in the distance between the iterators (copy constructions) if the elements are already sorted according to comp. For unsorted sequences, linearithmic (N*logN) in that distance (sorting,copy constructions).
For the copy constructor, linear in x's size (copy constructions).
See also
|