I have forgotten

• http://facebook.com/
• https://www.google.com/accounts/o8/id
• https://me.yahoo.com

# Map

Map and multimap containers are containers that manage key/value pairs as elements. View version details

### Key Facts

Gyroscopic Couple: The rate of change of angular momentum ( ) = (In the limit).
• = Moment of Inertia.
• = Angular velocity
• = Angular velocity of precession.

## Definition

The map template is defined in the standard header <map>, and in the nonstandard backward-compatibility header <map.h>.
#include <map>
namespace std {
template < class Key, class T,
class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > >
class map;
}

## Description

The map template associates, or maps, values of some key type to values of some other type. For example, it is possible to use a map to associate names represented as strings with some other type that you chose, such as floating-point values. This would allow you to associate a person's name with a bank account balance, grade point average, etc.

The main characteristics of a map are:

• each element has an unique key
• each element is composed of a key and a mapped value
• elements follow a strict weak ordering

A strict weak ordering is a binary relation on a set that is a strict partial order (a transitive relation that is irreflexive, or equivalently, that is asymmetric) in which the relation "neither nor " is transitive.

A short example declaring a map is:
map<string, double> aMap; // associates strings with doubles

## Performance

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

The asymptotic complexity of the operations that can be applied to maps are as follows:

Operation Complexity
Searching for an element O(log n)
Inserting a new element O(log n)
Incrementing/decrementing an iterator O(log n)
Removing a single map element O(log n)
Copying an entire map O(n)
Iterating through all elements O(n)

There is also a multimap container class where the keys do not have to be unique (a key can be associated with more than one value).

## Map Operations

Create, Copy, and Destroy Operations
Operation Effect
map m Creates an empty map without any elements
map m(op) Creates an empty map that uses op as the sorting criterion
map m1(m2) Creates a copy of another map of the same type (all elements are copied)
map m(beg,end) Creates a map initialized by the elements of the range [beg,end)
map m(beg,end,op) Creates a map with the sorting criterion op initialized by the elements of the range [beg,end)
m.~map() Destroys all elements and frees the memory

Map Effect
map< Key,el > A map that sorts keys with less<> (operator <)
map< Key,el,op > A map that sorts keys with op

Nonmodifying Operations of Maps
Operation Effect
m.size() Returns the actual number of elements in the container
m.empty() Returns if the container is empty (equivalent to size()==0)
m.max_size() Returns the maximum number of elements possible
m1==m2 Returns if m1 is equal to m2
m1!=m2 Returns if m1 is not equal to m2 (equivalent to !(m1==m2))
m1<m2 Returns if m1 is less than m2
m1>m2 Returns if m1 is greater than m2 (equivalent to m2<m1)
m1<=m2 Returns if m1 is less than or equal to m2 (equivalent to !(m2<m1))
m1>=m2 Returns if m1 is greater than or equal to m2 (equivalent to !(m1<m2))

Note: Operator [ ] (m[key]) returns a reference to the component of m whose key value is key, if this component exists. If this component does not exist, inserts a new component into m, the value of whose key is key, and for which the corresponding value is the default value of type el.

Special Search Operations of Maps
Operation Effect
count(key) Returns the number of elements with key key
find(key) Returns the position of the first element with key key or end()
lower_bound(key) Returns the first position where an element with key key would get inserted (the first element with key >= key)
upper_bound(key) Returns the last position where an element with key key would get inserted (the first element with key > key)
equal_range(key) Returns the first and last positions where elements with key key would get inserted (the range of elements with key == key)

Assignment Operations of Maps
Operation Effect
m1=m2 Assigns all elements of m2 to m1
m1.swap(m2) Swaps the data of m1 and m2
swap(m1,m2) Same (as global function)

Iterator Operations of Maps
Operation Effect
m.begin() Returns a bidirectional iterator for the first element (keys are considered const)
m.end() Returns a bidirectional iterator for the position after the last element (keys are considered const)
m.rbegin() Returns a reverse iterator for the first element of a reverse iteration
m.rend() Returns a reverse iterator for the position after the last element of a reverse iteration

Inserting and Removing Elements of Maps
Operation Effect
m.insert(elem) Inserts a copy of elem and returns the position of the new element and, for maps, if it succeeded
m.insert(pos,elem) Inserts a copy of elem and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search)
m.insert(beg,end) Inserts a copy of all elements of the range [beg,end)(returns nothing)
m.erase(elem) Removes all elements with value elem and returns the number of removed elements
m.erase(pos) Removes the element at iterator position pos (returns nothing)
m.erase(beg,end) Removes all elements of the range [beg,end)(returns nothing)
m.clear() Removes all elements

### References:

Example:
##### Example - accessing values of a map
Problem
This example of program creates an empty map, places three key/value pairs into it, then displays the values by accessing them via their keys.
Workings
#include <iostream>
#include <map>

using namespace std;

int main()
{
map<char, int> m;
if (m.empty())
cout << "\nThe created map is currently empty.";

m['x'] = 1;
m['y'] = 2;
m['z'] = 3;

cout << "\nAfter entering the three key/value pairs, the size of the map is now " << m.size();
cout << "\nIf the component key is x, the component value is "<< m['x'];
cout << "\nIf the component key is y, the component value is "<< m['y'];
cout << "\nIf the component key is z, the component value is "<< m['z'];

return 0;
}
Solution
Output:

The newly created map is currently empty.

After entering the three key/value pairs, the size of the map is now 3.

If the component key is x, the component value is 1
If the component key is y, the component value is 2
If the component key is z, the component value is 3
{"ft_lang_label":"__label__en","ft_lang_prob":0.7433453,"math_prob":0.96868205,"size":6330,"snap":"2019-43-2019-47","text_gpt3_token_len":1587,"char_repetition_ratio":0.15744546,"word_repetition_ratio":0.18015414,"special_character_ratio":0.25418642,"punctuation_ratio":0.09685431,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9770452,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,4,null,null,null,4,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-18T06:52:22Z\",\"WARC-Record-ID\":\"<urn:uuid:fec5990d-8b05-4524-84e8-1f70dcb216e9>\",\"Content-Length\":\"38058\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:15e2f85e-d714-4d5e-898b-3188f4c5efef>\",\"WARC-Concurrent-To\":\"<urn:uuid:9772f742-90d3-4ee0-92e6-707e1226c192>\",\"WARC-IP-Address\":\"192.155.228.11\",\"WARC-Target-URI\":\"https://www.codecogs.com/library/computing/stl/containers/associative/map.php\",\"WARC-Payload-Digest\":\"sha1:VEY4ISDM3ISUXXRUF7WP24XDUXTMDUAJ\",\"WARC-Block-Digest\":\"sha1:QZOXDPWLHVNWMJAA4JQ4HY6JMIKBLM4F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496669454.33_warc_CC-MAIN-20191118053441-20191118081441-00030.warc.gz\"}"}