Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Maps
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 37 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 37 of 50CamboFreelanceJune 19, 2026

C++ Maps

Learn C++ maps: key-value pair storage, insertion, lookup, deletion, and iteration. Compare map vs unordered_map.


Tutorials › C++ › Maps
Intermediate8 min readLesson 37 of 50

What is a Map?

A map stores key-value pairs where each key is unique and automatically sorted. Include <map>.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    map<string, int> wordCount;

    wordCount["hello"]  = 3;
    wordCount["world"]  = 2;
    wordCount["cpp"]    = 5;
    wordCount["hello"]++;   // increment existing

    for (const auto& [word, count] : wordCount) {
        cout << word << ": " << count << endl;
    }
    cout << "Contains 'cpp': " << wordCount.count("cpp") << endl;
    return 0;
}

Output (sorted by key):

cpp: 5
hello: 4
world: 2
Contains 'cpp': 1

Map Operations

OperationDescription
m[key] = valInsert or update
m.at(key)Access with bounds check
m.find(key)Returns iterator (end() if not found)
m.count(key)1 if key exists, 0 if not
m.erase(key)Remove by key
m.size()Number of entries
map<string, string> capitals;
capitals["Cambodia"] = "Phnom Penh";
capitals["France"]   = "Paris";
capitals["Japan"]    = "Tokyo";

if (capitals.find("Japan") != capitals.end())
    cout << "Japan capital: " << capitals["Japan"] << endl;

capitals.erase("France");
cout << "Size: " << capitals.size() << endl;   // 2
unordered_map: For faster average O(1) lookup (vs O(log n) for map), use unordered_map from <unordered_map>. It is not sorted but much faster for large datasets.

Exercise

Read a string of words from the user (space-separated) and count the frequency of each word using a map. Print the word frequency table.

Show Solution
#include <iostream>
#include <map>
#include <sstream>
using namespace std;

int main() {
    string line, word;
    cout << "Enter text: ";
    getline(cin, line);

    map<string, int> freq;
    istringstream iss(line);
    while (iss >> word) freq[word]++;

    for (const auto& [w, c] : freq)
        cout << w << ": " << c << endl;
    return 0;
}

Quiz

  1. What does map::count(key) return when the key exists?

    • A) The value associated with the key
    • B) 1
    • C) The number of duplicate keys
    • D) 0
    Answer

    B) 1 — since map keys are unique, count is always 0 or 1.

Summary

  • map<K,V> stores unique sorted key-value pairs.
  • Access: m[key] or m.at(key); search with find() or count().
  • Use unordered_map for O(1) average lookups without ordering.
← Previous: Vectors Next: Sets →
c++mapsstlintermediate
PreviousLesson 36: C++ VectorsNextLesson 38: C++ Sets
Back to All Tutorials