Hash container in C + + unordered_map using the example _c language

Source: Internet
Author: User

With the establishment of C++0X standard, C + + standard library also finally has a hash table this thing.

For a long time, the STL has only provided <map> as a container to store the corresponding relationship, the interior is usually implemented with a red-black tree, which is said to be due to various operations of the two-fork balance tree (such as the red-black tree), insert, delete, search, etc., are stable time complexity, that is, O (log n); But for the hash table, , because the performance problems caused by Re-hash can not be avoided, even if the performance of the hash table is very good in most cases, the instability caused by re-hash is intolerable at that time.

However, because of the performance advantage of the hash table, its use is still very wide, so the third party's class library basically provides support, such as Msvc

So we can now write the following code:

#include <iostream>
#include <string>
#include <unordered_map>
 
int main ()
{
 Std::unordered_map<std::string, int> months;
 months["January"] =;
 months["February"] =;
 months["march"] =;
 months["April"] =
 Months["may"] =;
 months["June"] =
 months["July"] =;
 months["August"] =;
 months["September"] =
 months["October"] =;
 months["November"] =
 months["December"] =;
 Std::cout << "September->" << months["September"] << Std::endl;
 Std::cout << "April   ->" << months["April"] << Std::endl;
 Std::cout << "December->" << months["December"] << Std::endl;
 Std::cout << "February->" << months["February"] << Std::endl;
 return 0;
}

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.