map .

The Basics Of Map In C++ With Examples

Written by Pauline Lafleur Feb 17, 2023 ยท 4 min read
The Basics Of Map In C++ With Examples

C++ is a powerful programming language that is widely used in developing various software and applications. One of its essential features is the map container, which is used to store key-value pairs. In this article, we will discuss the basics of map in C++ and provide examples to better understand its functionality.

Table of Contents

C++ StdMap Time Zones Map World
C++ StdMap Time Zones Map World from timezonesmapworld.blogspot.com

Introduction

C++ is a powerful programming language that is widely used in developing various software and applications. One of its essential features is the map container, which is used to store key-value pairs. In this article, we will discuss the basics of map in C++ and provide examples to better understand its functionality.

What is a Map?

A map is a container in C++ that stores key-value pairs. It is similar to a dictionary in Python or an associative array in PHP. The key is used to access the value, and both the key and value can be of any data type. Map is an ordered container, and the keys are unique, which means that duplicate keys are not allowed.

How to Declare a Map in C++

Before using a map, we must declare it in our program. The syntax for declaring a map is as follows:

std::map map_name;

For example, we can declare a map of integers and strings as follows:

std::map my_map;

How to Insert Values into a Map

To insert values into a map, we can use the insert() function. The syntax for inserting a value into a map is as follows:

map_name.insert(std::make_pair(key, value));

For example, to insert a key-value pair into our map, we can use the following code:

my_map.insert(std::make_pair(1, "apple"));

How to Access Values in a Map

To access the value of a key in a map, we can use the at() function or the square bracket notation. The syntax for accessing the value of a key in a map is as follows:

map_name.at(key);

or

map_name[key];

For example, to access the value of key 1 in our map, we can use the following code:

std::string fruit = my_map.at(1);

or

std::string fruit = my_map[1];

How to Check if a Key Exists in a Map

To check if a key exists in a map, we can use the count() function. The count() function returns 1 if the key exists in the map and 0 otherwise. The syntax for using the count() function is as follows:

map_name.count(key);

For example, to check if the key 1 exists in our map, we can use the following code:

if (my_map.count(1)) {

    // Key exists in map

}

How to Remove a Key-Value Pair from a Map

To remove a key-value pair from a map, we can use the erase() function. The syntax for removing a key-value pair from a map is as follows:

map_name.erase(key);

For example, to remove the key-value pair with the key 1 from our map, we can use the following code:

my_map.erase(1);

Map Example: Word Frequency Counter

Let's now look at an example of using a map in C++. Suppose we want to count the frequency of each word in a given text. We can use a map to store the words and their frequencies. Here's the code:

#include

#include

#include

int main()

{

    std::string text ="the quick brown fox jumps over the lazy dog";

    std::map word_freq;

    // Split the text into words

    std::string word;

    for (auto c : text) {

        if (c ==' ') {

            word_freq[word]++;

            word ="";

        }

        else {

            word += c;

        }

    }

    // Print the word frequencies

    for (auto p : word_freq) {

        std::cout << p.first << ": " << p.second << std::endl;

    }

    return 0;

}

In this example, we first declare a map called word_freq that will store the words and their frequencies. We then split the text into words and insert each word into the map using the square bracket notation. If the word already exists in the map, its frequency is incremented by 1. Finally, we print the word frequencies using a for loop that iterates over the map.

Conclusion

Maps are a powerful container in C++ that allow us to store key-value pairs. They are useful in various applications, such as word frequency counting, dictionary programs, and more. In this article, we discussed the basics of map in C++ and provided examples to better understand its functionality.

Question and Answer

Q: Can a map contain duplicate keys?

A: No, a map cannot contain duplicate keys. If we try to insert a key that already exists in the map, its value will be updated.

Q: What happens if we try to access a key that does not exist in the map?

A: If we try to access a key that does not exist in the map using the at() function, an exception will be thrown. If we use the square bracket notation, a new key-value pair will be inserted into the map with a default value (0 for integers, "" for strings, etc.).

Read next