Instructions and tips for using map containers in C + +

Source: Internet
Author: User

The map container in C + + provides a key-value pair container, and the difference between map and Multimap is only that multiple allows a key to correspond to multiple values.

I. Description of MAP
1 Header Files
#include <map>

2 Definitions
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<string,int> ("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 it is deleted.

5 Deleting data
(1) my_map.erase (MY_ITR);
(2) My_map.erase ("C");
It is also important to note that the first case cannot be deleted during the iteration, just as the element cannot be deleted while foreach.

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 () clears all elements
can be assigned and compared directly: =, >=, &lt, <=,! =, etc.

More advanced application Check help go, ^_^;

Examples of two/map

Requirements: Delete items with a greater than 100 of itemstruct in Mymap
struct ITEMSTRUCT
{
int A;
Char b[20];
};
Map<string, Itemstruct > Mymap.

Answer 1:
#include <iostream>
#include <ctime>
#include <map>
using namespace Std;
typedef struct ITEMSTRUCT
{
int A;
Char b[20];
}items;
ItemS S[4] = {{102, "what"},
{, "Hello"},
{198, "world"},
{"C + +"}
};;


int main ()
{
Map<string, items> Mymap;
String Str[4] = {"1st", "2nd", "3rd", "4th"};
for (int i = 0;   i<4; i++)
{
Mymap.insert (Make_pair (Str[i], s[i]));
}

Map<string,items>::iterator it;
For (It=mymap.begin (); It!=mymap.end (); it++)
{
if (it->second.a >100) {
I=mymap.erase (IT); -----> Correct
Mymap.erase (IT); ----->it failure.
}
}
First is key, second is value;
for (it = Mymap.begin (); It!=mymap.end (); it++)
{
cout<<it->first<< "" <<it->second.a<< "" <<it->second.b<<endl;
}
System ("pause");
return 0;
}

Answer 2:
#include <map>
#include <iterator>
#include <string>
#include <iostream>
#include <cstring>
using namespace Std;
struct ITEMSTRUCT
{
int A;
Char b[20];
itemstruct (int t,char*str)
{
a=t;
strcpy (B,STR);
}
};
int main ()
{
map<string,itemstruct>mymap;
Mymap.insert (Make_pair ("A", Itemstruct (Ten, "Hanzhou"));
Mymap.insert (Make_pair ("AB", Itemstruct ("Fuzhou")));
Mymap.insert (Make_pair ("abc", Itemstruct ("Zhengzhou")));
Mymap.insert (Make_pair ("ABCD", Itemstruct ("Wuhan")));
Mymap.insert (Make_pair ("ABCDE", Itemstruct ("Kunming")));
Mymap.insert (Make_pair ("ABCdef", Itemstruct ("Xiamen")));
Map<string,itemstruct>::iterator It=mymap.begin ();
while (It!=mymap.end ())
{
if ((It->second). a>100) Mymap.erase (it++);
else it++;
}
It=mymap.begin ();
while (It!=mymap.end ())
{
cout<<it->first<< "" << (It->second) .a<< "" << (It->second) .b<<endl;
it++;
}
System ("PAUSE");
return 0;
}

Answer 3:
  for (map<string,   itemstruct>::iterator   i    =   mymap.begin ();   i   !=    Mymap.end ();)  
  { 
          if ( i->second.a   >   100)  
                   i   =    Mymap.erase (i);  
          else 
                   ++i;  
  } 

Solution to compile error in compiling map in 4:VC6
Warnings similar to the following is generated even if you use the warning pragma to disable the warning:warning C4786: ' Std::rb_tree<caispanningtree<state,std::less<state>>::transclosurenode, CAiSpanningTree< State,std::less<state>>::transclosurenode,std::ident<cai spanningtree<state,std::less<state >>::transclosurenode,caispanningtree<s tate,std::less<state>>::transclosurenode>,std::less <caispanningtree<stat e,std::less<state>>::transclosurenode>> ': identifier was truncated to ' 255 ' characters in the debug information


Fix code added to the stdafx.h header file:

#pragma warning (disable:4786)

Http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html

Instructions and tips for using map containers in C + +

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.