The use of STL map (i)

Source: Internet
Author: User

A map is an associative container for storing key-value data. Where the key is the keyword to find, value is the actual stored value.

A specific keyword can only be associated with a unique value. A map is a sort structure composed of a pair of key values (Key/value),

The key value is read one without two (unique).

The map is usually implemented with a balanced binary lookup tree, so the map inserts, deletes, and finds the time complexity of the log (N). For

The massive data inserts and the query, the map is a good choice.

This article will explain the common operations of map. The improper place, welcome to criticize (bicheng.gui@gmail.com).

1. Map's Construction:

Map provides the following template constructors:

Template<class T1, Class t2>

Map (); Default constructor

Map (const map& m)//copy constructor

Map (iterator begin, iterator end); Interval constructors

Map (iterator begin, iterator end, const traits& _compare)//constructors with comparison predicates

Map (iterator begin, iterator end, const traits& _compare, const allocator& All)//with Splitter

Example:

#include <map>

#include <iostream>

using namespace Std;

typedef pair<int,int> Int_pair;

Map<int,int>::iterator itr1;

Map<int,int> M1; Default ascending, small to large

M1[1] = 10;

M1[2] = 20;

M1[3] = 30;

Output 10 20 30, with line wrapping

for (itr1 = M1.begin (); itr1!= m1.end (); ++itr1)

{

cout << itr1->second << Endl;

}

Map<int,int,greater<int>> m2; A descending map

Map<int,int,greater<int>>::iterator itr2;

M2[1] = 10; Inserts an element with a key value of 1,value 10, Multimap does not support operator[]

M2.insert (Int_pair (2,20)); The valuetype of map is pair (T1,T2);

M2[3] = 30;

Output 30,20,10

for (ITR2 = M2.begin (); itr2!= m2.end (); ++itr2)

{

cout << itr2->second << Endl;

}

Constructs a M3 using the M1 memory allocator

Map<int,int>::allocator_type al = M1.get_allocator ();

Map<int,int> m3 (less<int> (), AL);

Copy a M4 according to M1

Map<int,int> M4 (M1); Predicates and types must match.

Construct a map based on two iterator

Map<int,int,greater<int>> M5 (M2.begin (), M2.end ());

Output M5, 30,20,10, with line wrap.

for (ITR2 = M5.begin (); itr2!= m5.end (); ++itr2)

{

cout << itr2->second << Endl;

}

The above code needs to VS2005 the compiler above.

2. Map Common operations:

The map supports common iterator interfaces, such as Begin (), End (), Rbegin (), Rend ().

Empty () determines whether the map is empty. Return true for empty words.

Size () returns the length of the map

MaxSize () returns the maximum number of elements the map can hold, changing according to the compiler and platform.

3. Pair<iterator, bool> Insert (iterator Where,const value_type& val)

You can insert an object, a number of copies of an object, or a range of objects.

The Insert function inserts an object before the element that is referred to in the Where.

Sample code:

Map<int,int>::iterator itr1,itr2;

Map<int,int> m1,m2;

typedef pair<int,int> Int_pair;

M1.insert (Int_pair (1, 10));

M1.insert (Int_pair (3, 30));

Because the map container does not allow key values to repeat, after the insert operation, you can obtain the success of the operation based on the return value.

The return value is the pair of an iterator and a Boolean value that points to an element in the map that has that value, and a Boolean value that indicates whether the insert succeeded.

If the Boolean value is true to indicate that the insert succeeded, the iterator is the position of the new inserted value in the map.

If the Boolean value is False, which indicates that the insertion failed (the value already exists), the iterator is the position of the original value in the map.

Pair<map<int,int>::iterator, bool> Insertresult;

Insertresult = M1.insert (Int_pair (1,10));

if (Insertresult->second)

{

Insert Succeeded

}

Else

{

Insert failed

}


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.