DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
Associative Arrays in C++ - Map(C++)

# Using a Map

Indexing is the fundamental map operation: if m is of type Map<K,V> and x is of type K, then m[x] is an lvalue of type V. Thus we store a value v in m[x] by writing:

```   m[x] = v;
```

and retrieve it again by writing:

```   v = m[x];
```

If we ask for the value of m[x] without ever having specified one, we get the "default value" of type V: the value of an otherwise uninitialized static variable of type V It is very important that this is the value of a static variable in case V is a built-in type: the default value of an ordinary int variable is undefined but static int variables are initialized to zero. Thus after executing:

```   Map<String,int> m;

m["Morristown"]++;
```

m has one element with key Morristown and value 1.

Because evaluating m[k] creates an element of m if an appropriate element does not already exist, we need a way to test whether an element exists without creating one. Thus every map has a member function called element which returns an appropriate value:

```   if (m.element(k))
// m[k]  exists
```

Thus if we did not wish to rely on default values for map elements, we could write:

```   if (m.element(word))
m[word]++;
else
m[word] = 1;
```

The number of elements in a map m is m.size().

We can explicitly nominate a default value for map elements by passing that value to the map constructor. For example:

```   Map<String,String> s ("(undefined)");
```

declares s to be a map in which keys and values are both of type String and in which element values are initially (undefined).

A map entry can be removed from the map:

```   m.remove("Newark");
```

The element with the given key value is removed from the map. If there is no entry in the map that has this key, there is no effect.

Next topic: Copying a Map
Previous topic: Key and Value Types
{"ft_lang_label":"__label__en","ft_lang_prob":0.83838767,"math_prob":0.9859798,"size":1657,"snap":"2022-27-2022-33","text_gpt3_token_len":393,"char_repetition_ratio":0.13611615,"word_repetition_ratio":0.0,"special_character_ratio":0.24562462,"punctuation_ratio":0.108333334,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9687939,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-11T14:03:58Z\",\"WARC-Record-ID\":\"<urn:uuid:fdefd525-8ff5-483b-bd0f-1f21b0283b72>\",\"Content-Length\":\"5394\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:de5431a3-f331-4c3f-a980-89250cf76018>\",\"WARC-Concurrent-To\":\"<urn:uuid:1acbb046-4fa8-412f-af48-34dcd905ee4e>\",\"WARC-IP-Address\":\"132.147.224.138\",\"WARC-Target-URI\":\"http://osr600doc.xinuos.com/en/SDK_c++/_Using_a_Map.html\",\"WARC-Payload-Digest\":\"sha1:NKT66YZ2WKOVBQ6KDUPOB3W5URTQVN67\",\"WARC-Block-Digest\":\"sha1:BMVEH64UQ6D2BP7XVRTUCGW2IOLJ66ZP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571472.69_warc_CC-MAIN-20220811133823-20220811163823-00079.warc.gz\"}"}