Default comparison method of changing map in STL

Source: Internet
Author: User

As you know, the map at the bottom of the STL is implemented with a red-black tree, and its generic prototype is as follows:

Template <class _key, class _tp, Class _compare, class _alloc>
class Map {
              ...
              }

Where _key represents a comparison of the key (key), _TP represents the value, _compare represents a comparison, _alloc represents the memory allocator.

In general, when we write a map, we are always like writing the following code:

Map<int, char*>* my_map = new Map<int, char*>;

Indicates that the key is an int type and the value is a string type. The reason why _compare and _alloc are not limited is that int is a C + + built-in type, has the default comparison method, and _alloc also uses STL

The default memory scheme. But if you have the following structure:

struct term{
    char* str;
    int   hashcode;
};

Now we're going to take the term as the key to the map and assume that the term corresponds to the term frequency (int), so you can write this:

Map<term, int>* my_map = new Map<term, int>;

Obviously this is not working correctly because struct term is not a C + + built-in type, and does not know how to compare it by default. At this point you need to modify the default comparison method for the map:

Template <class t>
struct Compare
{
  int operator () (const t& x, const t& k) const{
    if ( X.hashcode >= K.hashcode) return 0;
    else return 1;
  }
};

In this case, the function object is used to load the comparison of the map, which means that the hashcode of term is used as the comparison method to find and insert the red-black tree.

So we can write the map in the following form:

map<term, int, compare<term> >* my_map = new map<term, int, compare<term> >;

This allows the map to function properly, such as insert operations:

Term my_term;
My_map->insert (Make_pair (my_term, 1));

But the above struct compare why to write such a form, write this line not:

Template <class t>
struct Compare
{
  int operator () (const t& x, const t& k) const{
    if ( X.hashcode >= K.hashcode) return 1;
    else return 0;
  }
};

This is not going to work. Why not, first look at the map of Find source code:

Template <class _key, class _value, Class _keyofvalue, 
          class _compare, class _alloc>
TypeName _key,_value,_keyofvalue,_compare,_alloc>::iterator 
_rb_tree<_key,_value,_keyofvalue,_compare,_alloc >::find (const _key& __k)
{
  _link_type __y = _m_header;      Last node which isn't less than __k. 
  _link_type __x = _m_root ();      Current node. 

  while (__x!= 0) 
    if (!_m_key_compare (_s_key (__x), __k))
      __y = __x, __x = _s_left (__x);
    else
      __x = _s_right (__x);

  Iterator __j = iterator (__y);   
  return (__j = = End () | | _m_key_compare (__K, _s_key (__j._m_node))? End (): __j;
}

_m_key_compare in the above code represents our comparison function object, _s_key (__x) represents the key of the __x node and compares it to the __k.

if (!_m_key_compare (_s_key (__x), __k))
      __y = __x, __x = _s_left (__x);

Indicates that if _s_key (__x) >= __k that is, if the node's key is greater than or equal to the lookup key, then the __x equals its left child node, or the right child node.

But why do not return directly when equal, but continue to find. For example:

If we are looking for a node with key 10 in the tree, start looking from the root node, then _m_key_compare returns 1 because of 8<10, and then

Turn to the right subtree of root and then, as 10==10,_m_key_compare returns 0, turn to the left subtree, but the Zuozi is empty and the loop stops.

return (__j = = End () | | _m_key_compare (__K, _s_key (__j._m_node))? End (): __j;

Since __j represents the "10" node (which is actually an iterator), because __k is 10, and __j._m_node's key is 10,_m_key_compare return 0, there are ternary operators.

At this point the return is __j, which means that it was found. So our comparison function object must be written as:

Returns 0 (false), or 1 (true), when the node key is greater than or equal to the key being found or inserted, which is determined by the internal source code.




From:http://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html

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.