The map of STL

Source: Internet
Author: User
Tags map class

The map container map is an associative container that represents a sequence of dual (key, value). It supports the key value of the unique key type and provides a quick retrieval of another key-based type T. Map also provides bidirectional iterators. The map container map, in standard C + +, corresponds to the map class and is defined in the <map> header file.

Mappings: Key → value, value = mapping [key] (like F:x→y,y = f (x)). That is, a value can be quickly positioned by a key by mapping.

Namespace Std {//taken from C++2003 standard

Template <class key, Class T, class Compare = Less<key>, class allocator = allocator< pair<const Key, t> > > class Map {

Public

Types: Type

typedef Key KEY_TYPE;

typedef T MAPPED_TYPE;

typedef pair<const Key, t> value_type;

typedef Compare Key_compare;

typedef allocator Allocator_type;

typedef typename Allocator::reference Reference;

typedef typename Allocator::const_reference Const_reference;

typedef implementation defined iterator;

typedef implementation defined Const_iterator;

typedef implementation defined Size_type;

typedef implementation defined Difference_type;

typedef typename Allocator::p ointer pointer;

typedef typename Allocator::const_pointer Const_pointer;

typedef std::reverse_iterator<iterator> Reverse_iterator;

typedef std::reverse_iterator<const_iterator> Const_reverse_iterator;

Class Value_compare:public Binary_function<value_type,value_type,bool> {

Friend class map;

Protected

Compare comp;

Value_compare (compare C): Comp (c) {}

Public

BOOL Operator () (const value_type& x, const value_type& y) Const {

Return Comp (X.first, Y.first);

}

};

Construct/copy/destroy: Construction/Copy/Destroy

Explicit map (const compare& comp = Compare (), const allocator& = allocator ());

Template <class inputiterator> map (inputiterator, inputiterator, const compare& comp = Compare (), con St allocator& = Allocator ());

Map (const map<key,t,compare,allocator>& x);

˜map ();

map<key,t,compare,allocator>& operator= (const map<key,t,compare,allocator>& x);

Allocator_type get_allocator () const;

Iterators: iterators

Iterator begin ();

Const_iterator begin () const;

Iterator End ();

Const_iterator end () const;

Reverse_iterator Rbegin ();

Const_reverse_iterator rbegin () const;

Reverse_iterator rend ();

Const_reverse_iterator rend () const;

Capacity: Capacity

BOOL empty () const;

Size_type size () const;

Size_type max_size () const;

Element access:

t& operator[] (const key_type& x);

Modifiers: How to Modify

Pair<iterator, bool> Insert (const value_type& x);

Iterator insert (iterator position, const value_type& x);

Template <class inputiterator> void Insert (Inputiterator-I, inputiterator last);

void Erase (iterator position);

Size_type Erase (const key_type& x);

void Erase (iterator, iterator last);

void swap (map<key,t,compare,allocator>&);

void Clear ();

Observers: Observer

Key_compare Key_comp () const;

Value_compare Value_comp () const;

Map Operations: Mapping operations

Iterator Find (const key_type& x);

Const_iterator Find (const key_type& x) const;

Size_type count (const key_type& x) const;

Iterator Lower_bound (const key_type& x);

Const_iterator lower_bound (const key_type& x) const;

Iterator Upper_bound (const key_type& x);

Const_iterator upper_bound (const key_type& x) const;

Pair<iterator,iterator>

Equal_range (const key_type& x);

Pair<const_iterator,const_iterator>

Equal_range (const key_type& x) const;

};

comparison operator Overloading:

Template <class Key, class T, Class Compare, class allocator> bool operator== (const map<key,t,compare,allocator& gt;& x, const map<key,t,compare,allocator>& y);

Template <class Key, class T, Class Compare, class allocator> bool operator< (const Map<key,t,compare,allocato r>& x, const map<key,t,compare,allocator>& y);

Template <class Key, class T, Class Compare, class allocator> bool operator!= (const map<key,t,compare,allocator& gt;& x, const map<key,t,compare,allocator>& y);

Template <class Key, class T, Class Compare, class allocator> bool Operator> (const Map<key,t,compare,allocato r>& x, const map<key,t,compare,allocator>& y);

Template <class Key, class T, Class Compare, class allocator> bool operator>= (const MAP<KEY,T,COMPARE,ALLOCAT or>& x, const map<key,t,compare,allocator>& y);

Template <class Key, class T, Class Compare, class allocator> bool operator<= (const MAP<KEY,T,COMPARE,ALLOCAT or>& x, const map<key,t,compare,allocator>& y);

Specialized algorithms: Special algorithm

Template <class Key, class T, Class Compare, class allocator>

void swap (map<key,t,compare,allocator>& x, map<key,t,compare,allocator>& y);

}

The value type of the map container is pair:

typedef pair<const Key, t> value_type;

The pair is the template structure of standard C + + defined in the <utility> header file:

Template<class Type1, class type2> struct pair {

typedef TYPE1 FIRST_TYPE;

typedef Type2 SECOND_TYPE

Type1;

Type2 second;

Pair ();

Pair (const type1& __VAL1, const type2& __VAL2);

Template<class Other1, class other2> pair (const pair<other1, other2>& _right);

};

To show the power of the mapping, we give an example of a word-counting program: (The word here is a generalized string separated by a white space)//WordCount.cpp

#include < iostream >

#include < FStream >

#include < map >

#include < string >

using namespace Std;



int main (int argc, char * argv []) ... {

typedef map < string, int > wordmap; Define a specific Word map type

typedef wordmap:: Iterator wmiter; To define an iterator of this type

Const char* fname = "WordCount.cpp"; Default filename string



if (argc > 1) fname = argv [1]; Reads the first argument in the command line as the filename path string

Ifstream in (fname); Open File input stream

if (! In) ... {//If an error is turned on, exit after displaying the message

cout << "Open file" << fname << "Error!" << Endl;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.