C + + Map common usage Instructions __c++

Source: Internet
Author: User

http://blog.csdn.net/shuzfan/article/details/53115922# three-take value

One declaration of two insert operations 1 use Insert for single and multiple inserts three values four capacity query five iterators Six remove Exchange 1 Remove 2 interchange Seven order comparison eight find nine operator

C + + map provides a key value to the container, where the data are in pairs, the following figure: the first value in each pair is called a keyword (key), each keyword can only appear once in the map, the second is called the corresponding value of the keyword.

—————————————————————————————————————————————— A. Statement

Header file
#include <map>

map<int, string> id_name;

Assignment with {} is started from c++11, so it will be an error when the compiler version is too low, such as Visual Studio
Map<int, string> id_name = {
                {2015, "Jim"},
                {2 016, "Tom"},
                {2017, "Bob"}};
1 2 3 4 5 6 7 8 9 10

—————————————————————————————————————————————— two. Insert Operation 2.1 use [] for a single insert

Map<int, string> id_name;

If a key value of 2015 is already present, the assignment modification is done, and if not, insert
id_name[2015] = "Tom";
1 2 3 4 2.1 Single and multiple inserts using Insert

There are 4 overloaded functions for an insert:

Inserts a single key value pair and returns the insertion position and success flag, when the insertion position already exists, the insert fails
pair<iterator,bool> Insert (const value_type& val);

Inserts at the specified position, the insertion efficiency at different positions is not the same because it involves rearrangement of
iterator inserts (const_iterator position, const value_type& val);

Inserts multiple
void inserts (Inputiterator-I, Inputiterator last);

C++11 begins to support using a list to insert multiple   
void inserts (initializer_list<value_type> il);
1 2 3 4 5 6 7 8 9 10 11

The following are examples of specific uses:

#include <iostream> #include <map> int main () {Std::map<char, int> mymap;
    Inserts a single value Mymap.insert (std::p air<char, int> (' A ', 100));

    Mymap.insert (std::p air<char, int> (' Z ', 200));
    Returns the insertion position and whether to insert a successful std::p Air<std::map<char, Int>::iterator, bool> ret;
    RET = Mymap.insert (std::p air<char, int> (' Z ', 500));
        if (Ret.second = = False) {std::cout << "element ' Z ' already existed";
    Std::cout << "with a value of" << ret.first->second << ' \ n ';
    }//Specify position to insert Std::map<char, int>::iterator it = Mymap.begin ();  Mymap.insert (IT, std::p air<char, int> (' B ', 300));  Higher efficiency Mymap.insert (IT, std::p air<char, int> (' C ', 400));
    Efficiency not highest//range multi-valued inserts Std::map<char, int> Anothermap;

    Anothermap.insert (Mymap.begin (), Mymap.find (' C '));

    The list form is inserted into the Anothermap.insert ({{' d ',}, {' E ', 200}}});
return 0; }
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 33

—————————————————————————————————————————————— three. Take a value

The element values in the map are primarily at and [], and at will be checked for subscript, and [] not.

Map<int, string> id_name;

There is no keyword 2016 in id_name, using [] to take a value will cause the insertion
//Therefore, the following statement will not error, but the print result is empty
cout<<id_name[2016].c_str () <<endl;

Keyword checking is done using at, so the following statement complains
id_name.at (2016) = "Bob";
1 2 3 4 5 6 7 8

—————————————————————————————————————————————— Four. Capacity Query

Query whether the map is an empty
bool empty ();

The number of key-value pairs in the query map
size_t size ();

The maximum number of key values that the query map can contain is related to the system and the application library.
//In addition, this does not mean that the user must be able to save so much, it is likely that the memory has failed to reach the
size_t max_size ();

The number of elements that the query keyword is key to, in the map result is not 0 namely 1
size_t count (const key& Key) const;
1 2 3 4 5 6 7 8 9 10 11-12

—————————————————————————————————————————————— five. iterators

There are eight functions to get iterators: * Begin, end, rbegin,rend*, and corresponding * Cbegin, cend, crbegin,crend*.

The difference is that the latter must return const_iterator, while the former returns iterator or const_iterator based on the type of map. In the const case, the value is not allowed to be modified. As shown in the following code:

Map<int,int>::iterator it;
Map<int,int> mmap;
Const map<int,int> Const_mmap;

it = Mmap.begin (); Iterator
Mmap.cbegin ();//const_iterator

const_mmap.begin ();//const_iterator
Const_mmap.cbegin (); Const_iterator
1 2 3 4 5 6 7 8 9

The returned iterator can perform a subtraction operation, in addition, if the map is empty, begin = end.

—————————————————————————————————————————————— six. Delete swap 6.1 Delete

//delete the key-value pair that the iterator points to, and return an iterator to the next element iterator erase (iterator POS)//delete a certain range of elements and return an iterator iterator erase to the next element (Const_ite

Rator, Const_iterator last);

According to the key to delete, return the number of deleted elements, in the map results are not 0 or 1 size_t erase (const key_type& key); Empty the map and empty the size to 0 

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.