Category: containers

Component type: type

Description

Map is a Sorted Associative Container that associates objects of type Key with objects of type Data. Map is a Pair Associative Container, meaning that its value type is pair<const Key, Data>. It is also a Unique Associative Container, meaning that no two elements have the same key.

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

Example

struct ltstr {

 bool operator()(const char* s1, const char* s2) const {

  return strcmp(s1, s2) < 0;

 }

};

int main() {

 map<const char*, int, ltstr> months;

 months['january'] = 31;

 months['february'] = 28;

 months['march'] = 31;

 months['april'] = 30;

 months['may'] = 31;

 months['june'] = 30;

 months['july'] = 31;

 months['august'] = 31;

 months['september'] = 30;

 months['october'] = 31;

 months['november'] = 30;

 months['december'] = 31;

 cout << 'june –> ' << months['june'] << endl;

 map<const char*, int, ltstr>::iterator cur = months.find('june');

 map<const char*, int, ltstr>::iterator prev = cur;

 map<const char*, int, ltstr>::iterator next = cur;

 ++next;

 --prev;

 cout << 'Previous (in alphabetical order) is ' << (*prev).first << endl;

 cout << 'Next (in alphabetical order) is ' << (*next).first << endl;

}

Definition

Defined in the standard header map, and in the nonstandard backward-compatibility header map.h.

Template parameters
Parameter Description Default
Key The map's key type. This is also defined as map::key_type.
Data The map's data type. This is also defined as map::data_type.
Compare The key comparison function, a Strict Weak Ordering whose argument type is key_type; it returns true if its first argument is less than its second argument, and false otherwise. This is also defined as map::key_compare. less<Key>
Alloc The map 's allocator, used for all internal memory management. alloc
Model of

Unique Sorted Associative Container, Pair Associative Container

Type requirements

• Data is Assignable.

• Compare is a Strict Weak Ordering whose argument type is Key.

• Alloc is an Allocator.

Public base classes

None.

Members
Member Where defined Description
key_type Associative Container The map 's key type, Key.
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату