.insert()

Elements can be added to a map by inserting key-value pairs using the .insert() method.

Syntax

A pair can be inserted using std::pair which is a class template used to store two different objects as a single unit.

mapName.insert(std::pair<type1, type2>(key, value));

Respectively, type1 and type2 are the data types of the key and value that will be inserted.

Codebyte Example

The following codebyte example inserts different mammals and their average lifespans into the lifeSpan map:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy