Std::map

Source: Internet
Author: User

1. The elements in map are actually a pair.
2. Map key can not be a pointer, such as int*, char* and so on, will be wrong. The common use is string, int is OK.
3. Map is a disordered container, and vector is ordered. The so-called ordered disorder refers to the elements are not placed in a certain order, but disorderly order, random storage (is mapped after the approximate random storage). So there is some efficiency difference when traversing.
4. Determine if there is any content that can be found in this key:
Std::map<std::string,record>::const_iterator Citer;
Citer = Stdfile.m_map.find (s);
if (Citer = = Stdfile.m_map.end ())//not found is pointing to end.
{
M_vecmorefile.push_back (s);
}
If the content of the key is a pointer, it should be judged with a null pointer.
5. Traverse:
Std::map<std::string,record>::iterator ITER;
for (iter = M_map.begin (); Iter!= m_map.end (); iter++)
{
std::string s = iter->second.filename;
}
Because the map content can be quite a pair, it is simple, with Iter->second can get the value.

There are a few other uses that you can turn to:
1 Header Files
#include <map>

2 definition
Map<string, int> My_map;
or a typedef map<string, int> My_map;
My_map My_map;

3 Inserting data
(1) my_map["a"] = 1;
(2) My_map.insert (map<string, Int>::value_type ("B", 2));
(3) My_map.insert (pair<string,int> ("C", 3));
(4) My_map.insert (Make_pair ("D", 4));

4 finding data and modifying data
(1) int i = my_map["a"];
My_map["a"] = i;
(2) My_map::iterator My_itr;
My_itr.find ("B");
Int J = my_itr->second;
My_itr->second = j;
Note, however, that the key itself cannot be modified unless deleted.

5 Deleting data
(1) my_map.erase (MY_ITR);
(2) My_map.erase ("C");
Note that the first situation cannot be deleted during the iteration, as is the case when foreach cannot delete the element.

6 Iterative data
For (My_itr=my_map.begin (); My_itr!=my_map.end (); ++my_itr) {}

7 Other methods
My_map.size () returns the number of elements
My_map.empty () to determine if it is empty
My_map.clear () Empty all elements
Can be directly assigned and compared: =,, >=, <=,!=, etc.

Traverse:

#include <iostream>
#include <map>
using namespace Std;

int main () {
map<int,int>m;
m[1]=2;
m[2]=3;
Map<int,int>::iterator ITER;
for (iter = M.begin (); Iter!= m.end (); iter++)
{
cout<<iter->first<< "" <<iter->second<<endl;
}
return 0;
}

Summarize:

Find ();

Erase ();

Std::map<std::string, int> map;

Map[std::string] = int;

First parameter

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.