Cmap introduction and simple instances in VC ++!

Source: Internet
Author: User

I recently used a dictionary in MFC, so I won't check the information on the Internet. Summary:
1. What is cmap?
Map, also known as Dictionary, is an element list composed of a key and its corresponding element value.

Element form set. Cmap is a template class of MFC. You can create a variable of any type to another

The ing of variables (MAP) uses hash tables for storage, so it is faster. Generally, arrays are used for fast search and add/delete operations.

It is convenient to use the linked list, but if the two are integrated, it is best to use the Hexi table.

 

2. Notes:
1. How to declare cmap
Many people are confused about the cmap declaration mode cmap <key, arg_key, value, arg_value>. Why not use cmap <key, value>

What about it? In fact, the data in cmap will eventually be cpair, while the data in cpair is (Key, value ). Therefore, cmap actually stores keys.

Instead of arg_key. However, if you view the source code of MFC, almost all the internal parameters of cmap are transmitted to access arg_key and arg_value. Therefore, it seems correct to use key & to replace arg_key, except in these cases:
1. Simple data types, such as int and char, are used for value transfer and parameter transfer.
2. If cstring is used as the key, you should use lpctstr for arg_key instead of cstring &.

2. What are the typical operations related to map:

1. insert element units with given keywords into map.

2. Search for element units with given keywords in map.

3. Delete element units with given keywords in map.

4. enumerate (traverse) All element units in the map.


3. Simple Example:
Example 1: Let's look at the usage of a cmap. The following sample code:

Cmap <int, Int &, cpoint, cpoint &> mymap;

// Initialize the hash table and specify its size (odd number ). Mymap. inithashtable (257 );

// Add element units to mymap.
For (INT I = 0; I <200; I ++)
Mymap. setat (I, cpoint (I, I ));

// Delete the element unit corresponding to the keyword with an even number.
Position Pos = mymap. getstartposition ();
Int nkey;
Cpoint pt;
While (Pos! = NULL)
{
Mymap. getnextassoc (Pos, nkey, pt );

If (nkey % 2) = 0)
Mymap. removekey (nkey );
}

# Ifdef _ debug
Afxdump. setdepth (1 );
Afxdump <"mymap:" <& mymap <"/N"; cmap <int, Int &, cpoint, cpoint &> mymap;

// Initialize the hash table and specify its size (odd number ). Mymap. inithashtable (257 );

// Add element units to mymap.
For (INT I = 0; I <200; I ++)
Mymap. setat (I, cpoint (I, I ));

// Delete the element unit corresponding to the keyword with an even number.
Position Pos = mymap. getstartposition ();
Int nkey;
Cpoint pt;
While (Pos! = NULL)
{
Mymap. getnextassoc (Pos, nkey, pt );

If (nkey % 2) = 0)
Mymap. removekey (nkey );
}

# Ifdef _ debug
Afxdump. setdepth (1 );
Afxdump <"mymap:" <& mymap <"/N ";


Cmap is a good data structure, especially when you build a dictionary. For example, idcountry indicates "China", which is a tuples.

, That is, a pair, the key is "idcountry", and the value is "China ".

Example 2:

1. Define a cmap and add data items (key-value pairs) to the cmap ).
Cmap <cstring, lpctstr, cstring, lpctstr> m_itemmap;
Cstring strkey = _ T (""), STR = _ T ("");
Int I;
For (I = 0; I <5; I ++)
{
Strkey. Format ("% d", I); // This is the key
Str. Format ("A % d", I); // key value
M_itemmap.setat (strkey, STR );
}
2. Common Methods for traversing a cmap.
Position Pos = m_itemmap.getstartposition ();
While (POS)
{
M_itemmap.getnextassoc (Pos, strkey, STR );
Cout <strkey <":" <STR <Endl;
}
3. Search for corresponding data items in cmap.
Cstring preset;
If (m_itemmap.lookup ("1", preset ))
{
Cout <preset <Endl;
}

Post: http://blog.csdn.net/wjhuangjin/article/details/4823661

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.