C + + Standard library: Std_map as an associative array

Source: Internet
Author: User
Tags map class

 

abstract : A typical application of std::map as a container is to function as an associative array. In languages such as Java and so on, associative arrays exist extensively. Std::map is a container that has two words in its conceptual framework: keys and values, std::map a key relative to a value, which is equivalent to a dictionary that corresponds to an index and a person's content. In general, Std::map is implemented with a balanced binary tree, so most of its operations can be done in log (n) time. Let's take a look at some of the simple uses:

#include <map>
Define an integer-to-integer mapping
One of the previous keys, the latter one is the value
Std::map<int, int> map;

To create a simple mapping, you can
The following two sentences create two nodes in the map and complete the expected mappings separately.
map[0]=12;
map[99]=13;

Std::map is to store the elements in the form of nodes, we can manually create a node and then put in the container or delete the related conditions from the container node, which is driven by the "key", that is, an operation you often want to tell Std::map is for which key.

Here are some typical code:

#include <map>
#include <string>
#include <utility>
#include <iostream>

Std::map<std::string, int> String_int;
string_int["one"]=12;
string_int["Name"]=88;
Another way to insert an element
String_int.insert (Std::make_pair (std::string ("OK"), 0);

Search for the specified key
Std::map<std::string, Int>::const_iterator b=string_int.find ("OK");
If you find
if (B!=string_int.end ())
{
Std::cout << "OK:" << *b << Std::endl;
}

Deletes the specified element, depending on the iterator
String_int.erase (b);
Deletes the specified element, depending on the key, returns the number of deletions
Only 0 or 1 for std::map, but not necessarily for std::mutimap.
size_t n=string_int.erase ("one");C + + 's Std::map class is accessed through []#pragma warning (disable:4786)#include <map> #include <string> #include <iostream> int main () {using namespace std; map<int, string  > names; Element append Names.insert (Map<int, String>::value_type ("AAA")); Names.insert (Map<int, String>::value_type ("CCC"); Names.insert (Map<int, String>::value_type ("eee")) Names.insert (Map<int, String>::value_type (40, " DDD ")); Names.insert (Map<int, String>::value_type (" BBB ")); Change value names[50] = "FFF";//output cout << names[10] << endl;cout << names[20] << endl;cout << name S[30] << endl;cout << names[40] << endl;cout << names[50] << Endl;     return 0;} Original from: http://blog.sina.com.cn/s/blog_601996e40100tj57.html original author for Aria steamed. Please respect the copyright of the original author

C + + Standard library: Std_map as an associative array

Related Article

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.