STL map usage

Source: Internet
Author: User
STL map usage from: http://www.ecjtu.org/forum/htm_data/59/0712/14427.html 1. The elements in map is actually a pair.
2. Map keys generally cannot be pointers, such as int * or char *, which may lead to errors. Commonly Used strings are used, and INT is also used.
3. map is an unordered container, while vector is ordered. the so-called ordered disorder means that the elements are not put in a certain order, but randomly stored in a disordered Order (approximately randomly stored after ing ). so there are some efficiency differences during traversal.
4. Check whether the key content is found:
STD: Map <STD: String, record >:: const_iterator citer;
Citer = stdfile. m_map.find (s );
If (citer = stdfile. m_map.end () // if it is not found, it directs to end.
{
M_vecmorefile.push_back (s );
}
If the key content is a pointer, the NULL pointer can also be used to determine.
5. traversal:
STD: Map <STD: String, record >:: iterator ITER;
For (iter = m_map.begin (); iter! = M_map.end (); ITER ++)
{
STD: String S = ITER-> second. filename;
}
Since the map content can be equivalent to a pair, it is easy to use ITER-> second to get the value.

By the way, you can refer to the following usage:
1 header file
# Include <map>

2 Definitions
Map <string, int> my_Map;
Or typedef map <string, int> MY_MAP;
MY_MAP my_Map;

3. insert 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. Search for and modify 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;
However, note that the key itself cannot be modified unless it is deleted.

5. delete data
(1) my_Map.erase (my_Itr );
(2) my_Map.erase ("c ");
Note that the first case cannot be deleted during iteration. The reason is that the elements cannot be deleted in foreach mode.

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 () determines whether it is null
My_map.clear () Clear all elements
You can directly assign values and compare values: =,>, >=, <, <= ,! = And so on

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.