请输入您要查询的百科知识:

 

词条 map::map
释义

Constructs a map that is empty or that is a copy of all or part of some other map.

map( );

explicit map(

const Traits& _Comp

);

map(

const Traits& _Comp,

const Allocator& _Al

);

map(

const map<Key, Type, Traits, Allocator>& _Right

);

template<class InputIterator>

map(

InputIterator _First,

InputIterator _Last

);

template<class InputIterator>

map(

InputIterator _First,

InputIterator _Last,

const Traits& _Comp

);

template<class InputIterator>

map(

InputIterator _First,

InputIterator _Last,

const Traits& _Comp,

const Allocator& _Al

);

Parameters

_Al The storage allocator class to be used for this map object, which defaults to Allocator.

_Comp The comparison function of type constTraits used to order the elements in the map, which defaults to hash_compare.

_Right The map of which the constructed set is to be a copy.

_First The position of the first element in the range of elements to be copied.

_Last The position of the first element beyond the range of elements to be copied.

Remarks

All constructors store a type of allocator object that manages memory storage for the map and that can later be returned by calling get_allocator. The allocator parameter is often omitted in the class declarations and preprocessing macros used to substitute alternative allocators.

All constructors initialize their map.

All constructors store a function object of type Traits that is used to establish an order among the keys of the map and that can later be returned by calling key_comp.

The first three constructors specify an empty initial map, the second specifying the type of comparison function (_Comp) to be used in establishing the order of the elements and the third explicitly specifying the allocator type (_Al) to be used. The key word explicit suppresses certain kinds of automatic type conversion.

The fourth constructor specifies a copy of the map _Right.

The last three constructors copy the range [_First, _Last) of a map with increasing explicitness in specifying the type of comparison function of class Traits and allocator.

Example

// map_map.cpp

// compile with: /EHsc

#include <map>

#include <iostream>

int main( )

{

using namespace std;

typedef pair <int, int> Int_Pair;

map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter;

map <int, int, greater<int> >::iterator m2_Iter;

// Create an empty map m0 of key type integer

map <int, int> m0;

// Create an empty map m1 with the key comparison

// function of less than, then insert 4 elements

map <int, int, less<int> > m1;

m1.insert( Int_Pair( 1, 10 ) );

m1.insert( Int_Pair( 2, 20 ) );

m1.insert( Int_Pair( 3, 30 ) );

m1.insert( Int_Pair( 4, 40 ) );

// Create an empty map m2 with the key comparison

// function of geater than, then insert 2 elements

map <int, int, greater<int> > m2;

m2.insert( Int_Pair( 1, 10 ) );

m2.insert( Int_Pair( 2, 20 ) );

// Create a map m3 with the

// allocator of map m1

map <int, int>::allocator_type m1_Alloc;

m1_Alloc = m1.get_allocator( );

map <int, int> m3( less<int>( ), m1_Alloc );

m3.insert( Int_Pair( 3, 30 ) );

// Create a copy, map m4, of map m1

map <int, int> m4( m1 );

// Create a map m5 by copying the range m1[_First, _Last)

map <int, int>::const_iterator m1_bcIter, m1_ecIter;

m1_bcIter = m1.begin( );

m1_ecIter = m1.begin( );

m1_ecIter++;

m1_ecIter++;

map <int, int> m5(m1_bcIter, m1_ecIter);

// Create a map m6 by copying the range m4[_First, _Last)

// and with the allocator of map m2

map <int, int>::allocator_type m2_Alloc;

m2_Alloc = m2.get_allocator( );

map <int, int> m6( m4.begin( ), ++m4.begin( ), less<int>( ), m2_Alloc);

cout << "m1 =";

for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )

cout << " " << m1_Iter -> second;

cout << endl;

cout << "m2 =";

for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )

cout << " " << m2_Iter -> second;

cout << endl;

cout << "m3 =";

for ( m3_Iter = m3.begin( ); m3_Iter != m3.end( ); m3_Iter++ )

cout << " " << m3_Iter -> second;

cout << endl;

cout << "m4 =";

for ( m4_Iter = m4.begin( ); m4_Iter != m4.end( ); m4_Iter++ )

cout << " " << m4_Iter -> second;

cout << endl;

cout << "m5 =";

for ( m5_Iter = m5.begin( ); m5_Iter != m5.end( ); m5_Iter++ )

cout << " " << m5_Iter -> second;

cout << endl;

cout << "m6 =";

for ( m6_Iter = m6.begin( ); m6_Iter != m6.end( ); m6_Iter++ )

cout << " " << m6_Iter -> second;

cout << endl;

}

out put

m1 = 10 20 30 40m2 = 20 10m3 = 30m4 = 10 20 30 40m5 = 10 20m6 = 10

Header: <map>

Namespace: std

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/12/23 1:21:26