.insert()
Published May 10, 2022Updated May 15, 2024
Contribute to Docs
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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours