Create an associated container using the <map> library

Source: Internet
Author: User

Absrtact: When an index is an integral type, it is not difficult to associate a value with it, but what if the correlation value of the data is other data types? The <map> library has an associative container that can be easily associated with all types of data pairs. This article will discuss the usage and techniques of <map> library.

relational databases, scientific computing applications, and web-based systems often require a vector-like container whose index can be a data type, not necessarily an integer. Such containers are called associative containers, or map. For example, a directory service application can store a private name as an index, and the phone number as its associated value:

directory["Harry"]=8225687;// 插入 "Harry" 并与他的电话号码关联
iterator it=directory.find("Harry");// 获取 Harry 的电话号码

Other associated container applications also include mapping URLs to IP DNS servers, dictionaries, inventory lists, payroll tables, and more. So how to break through the limitation of Integer index and implement the associative container with other data types as index? The answer is: Use the <map> Library to create and process the associated container.

Pair and Map

In a recent article, I introduced the concept of tuple, which is a collection of different types of elements. One of the things that is not mentioned in this article is that the C++98 standard library already has a special tuple type--pair. It associates the key value (that is, the first element) with a value (the second value). For example:

#include <utility> //definition of pair
#include <string>
pair <string, string> prof_and_course("Jones", "Syntax");
pair <int, string> symbolic_const (0, "false");

The standard library also defines an auxiliary function to facilitate the creation of pair types:

string prof;
string course;
make_pair(prof,course);//returns pair <string,string>

First step: Construct and initialize a map object

Suppose you are developing an address book program that contains the name and e-mail address of the Address Book. Class template map defines I in <map>, which is an associative container that uses type pairs, the first element is an index, and the second element is the associated value. Use the following methods:

#include <map>
map <string, string> addresses;

To add an element, use the subscript operator:

addresses["Paul W."]="paul@mail.com";

Here, the string "Paul W." is the index or key value, and "paul@mail.com" is the value associated with it. If the map already contains this key value, the currently associated value does not change:

addresses["Paul W."]=
"newaddr@com.net"; // 不起作用

Step Two: Search

Without inserting an element, you can use the Find () member function if you want to check whether an element exists. Find () has two overloaded versions:

iterator find(const key_type& k);
const_iterator find(const key_type& k) const;

Typically, you can make your code more readable by using a typedef:

typedef map <string, string>::const_iterator CIT;
CIT cit=addresses.find("Paul W.");
if (cit==addresses.end())
  cout << "sorry, no such key" << endl;
else
  cout << cit->first << ''\t'' << cit->second << endl;

Cit->first and Cit->second in an expression return the key value and its associated value, respectively.

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.