In C++, a map is a container that stores elements in key-value pairs. It is part of the Standard Template Library (STL) and is implemented as a balanced binary tree. The keys in a map are unique, and the values can be accessed using these keys.
Maps automatically sort their elements by key, allow fast retrieval of values using keys, and provide iterators to traverse through the elements.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age;
age["Alice"] = 30;
age["Bob"] = 25;
cout << "Alice's age: " << age["Alice"] << endl;
return 0;
}
Console Output:
Alice's age: 30
Elements can be inserted into a map using the insert() function or by using the subscript operator [].
You can access elements by their keys using the subscript operator []. If the key does not exist, a new entry is created.
Elements can be removed using the erase() function, which takes a key or an iterator as an argument.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age;
age.insert(make_pair("Charlie", 35));
age["David"] = 40;
age.erase("Charlie");
cout << "David's age: " << age["David"] << endl;
return 0;
}
Console Output:
David's age: 40
Maps provide iterators to traverse through all elements. You can use a for loop with iterators to access each key-value pair.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Eve", 28}, {"Frank", 33}};
for (auto it = age.begin(); it != age.end(); ++it) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}
Console Output:
Eve: 28
Frank: 33
The find() method returns an iterator to the element if the key exists, or the end iterator if it does not.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Grace", 29}, {"Hank", 31}};
if (age.find("Grace") != age.end()) {
cout << "Grace is found." << endl;
} else {
cout << "Grace is not found." << endl;
}
return 0;
}
Console Output:
Grace is found.
The size() method returns the number of elements in the map.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Ivy", 24}, {"Jack", 27}};
cout << "Number of people: " << age.size() << endl;
return 0;
}
Console Output:
Number of people: 2
The clear() method removes all elements from the map, leaving it empty.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Karen", 32}, {"Leo", 29}};
age.clear();
cout << "Map size after clear: " << age.size() << endl;
return 0;
}
Console Output:
Map size after clear: 0
You can create a copy of a map using the copy constructor.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Mona", 26}, {"Nate", 34}};
map<string, int> ageCopy(age);
cout << "Copy size: " << ageCopy.size() << endl;
return 0;
}
Console Output:
Copy size: 2
The swap() method exchanges the contents of two maps.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age1 = {{"Olivia", 22}};
map<string, int> age2 = {{"Paul", 45}};
age1.swap(age2);
cout << "Age1 size after swap: " << age1.size() << endl;
return 0;
}
Console Output:
Age1 size after swap: 1
You can define a custom sorting criterion for a map by using a comparator function.
#include <iostream>
#include <map>
using namespace std;
struct CustomCompare {
bool operator()(const string &a, const string &b) const {
return a.length() < b.length();
}
};
int main() {
map<string, int, CustomCompare> age = {{"Quinn", 23}, {"Riley", 29}};
for (auto it = age.begin(); it != age.end(); ++it) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}
Console Output:
Quinn: 23
Riley: 29
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies