Map and Unordered_map with char * as key

Source: Internet
Author: User
Tags strcmp

Map and Unordered_map with char * as key

Using map or Unordered_map to search for strings is generally used std::string type as key, but std::string efficiency is too low, have to optimize, try to use char* as key to find.

First, map to char* as key

The default Map<char *,int> key is the pointer, which compares the size of the pointer value and does not match the string.

To view the template definition of a map: (HTTP://WWW.CPLUSPLUS.COM/REFERENCE/MAP/MAP/?KW=MAP)

Template < class Key,//map::key_type

Class T,//map::mapped_type

Class Compare = Less<key>,//Map::key_compare

Class Alloc = Allocator<pair<const key,t> >//Map::allocator_type

> class map;

So we can write a comparison class of strings to implement the map with char * as the key, the code is as follows:

struct CMP

{

Overloaded operators

BOOL Operator () (const char * Str1,constchar * str2)

{

Return strcmp (STR1,STR2) < 0;

}

};

Map<char*,int,cmp> Mapstr;

inserting char * in MAPSTR can be used to find operations of strings;

Remember that if you are a local pointer variable, you may have random results, so it is recommended that you use char *ss = new Char[n] To make space requests for pointers with new.

Second, unordered_map with char * as key

Unordered_map is a hash table in c++11, because Hash_map already exists, so it makes a strange name.

Unordered_map template Definition

(HTTP://WWW.CPLUSPLUS.COM/REFERENCE/UNORDERED_MAP/UNORDERED_MAP/?KW=UNORDERED_MAP):

Template < class Key,//Unordered_map::key_type
Class T,//Unordered_map::mapped_type
Class Hash = Hash<key>,//Unordered_map::hasher
Class Pred = Equal_to<key>,//Unordered_map::key_equal
Class Alloc = allocator< pair<const key,t> >//Unordered_map::allocator_type
> class unordered_map;

Unordered_map need to write down the hash function and the comparison function, as follows:

hash function

struct HASH_FUNC

{

BKDR hash algorithm, about the string hash function, you can look for information to see

int operator () (PSA_CHAR * str) const

{

int seed = 131;//31 131 1313 13131131313 etc//

int hash = 0;

while (*STR)

{

hash = (hash * seed) + (*STR);

STR + +;

}

Return hash & (0X7FFFFFFF);

}

};

comparison function

struct CMP

{

BOOL Operator () (const Psa_char *str1,const Psa_char * str2) const

{

Return strcmp (str1,str2) = = 0;

}

};

Unordered_map<char*,int, Hash_func,cmp> hashstr;

Compiling unordered_map is required with the compile option –std=c++0x.

The purpose of overriding the hash function and the comparison function should be that only the hash value is the same and the string is the same to determine that two strings are the same, and if only the hash value is the same, it is not possible to determine the same string.

Remember: Be sure to pay attention to the problem of the local pointer variable, use the new operator to request space on the pointer, and use the end traversal map or unordered_map to free up space.

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.