Table of Contents
grammar
Example
Output
algorithm
Initialize the map using the initialization list
Use assignment operator to initialize mapping
Initialize a map from another map
in conclusion
Home Backend Development C++ C++ program initialization dictionary

C++ program initialization dictionary

Sep 09, 2023 pm 07:01 PM
dictionary program initialization

C++ program initialization dictionary

C differs from Python in terms of the dictionary with the same name, but it has the same data structure with similar functionality. C supports mapping, which can be used in the STL class std::map. The map object contains a pair of values ​​in each entry, one is the key value and the other is the map value. Key values ​​are used to search for and uniquely identify entries in the map. While mapped values ​​are not necessarily unique, key values ​​must always be unique in the map. Let's take a look at how to use mapping.

First, let's see how to define a mapping data structure in C.

grammar

#include <map>
map <data_type 1, data_type 2> myMap;
</map>
Copy after login

Let’s take an example and see how to do this −

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //initialising the map
   map <int, string> myMap;

   //inserting two key-value pairs
   myMap.insert({1, "Hello"});
   myMap.insert({2, "World"});

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << endl;
   }

   return 0;
}
Copy after login

Output

1 Hello
2 World
Copy after login

In C, maps (Maps) can be initialized in different ways. The algorithm is very simple.

algorithm

  • Create a map object.

  • Assign a value to an object when it is declared.

Initialize the map using the initialization list

Using an initialization list to initialize a map is the same as initializing an array in C. We just need to assign key-value pairs when declaring the mapping, enclosed in curly braces, in the format {key, value}. The syntax is as follows.

grammar

#include <map>
map <data_type 1, data_type 2> myMap = {{key1, value1}, {key2, value2}};
Copy after login

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //initialising the map
   map <int, string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}
Copy after login

Output

1 One
2 Two
3 Three
Copy after login
Copy after login
Copy after login

Use assignment operator to initialize mapping

This is similar to assigning a value to a specific index in an array. We didn't mention the index, but put the key values ​​in the map subscript, just like in an array.

grammar

#include <map>
map <data_type 1, data_type 2> myMap;
myMap[key1] = value1;
</map>
Copy after login

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //declaring the map
   map <int, string> myMap;
   myMap[1] = "One";
   myMap[2] = "Two";
   myMap[3] = "Three";

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}
Copy after login

Output

1 One
2 Two
3 Three
Copy after login
Copy after login
Copy after login

Initialize a map from another map

It may be necessary to copy a map into another map, so we can initialize a map from another map. We take advantage of the map class's copy constructor by passing the map object to the map's copy constructor at declaration time.

grammar

#include <map>
map <data_type 1, data_type 2> myMap1(myMap2);
Copy after login

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //declaring the map
   map <int, string> myMap;
   myMap[1] = "One";
   myMap[2] = "Two";
   myMap[3] = "Three";

   //copying using copy constructor
   map <int, string> myMap2(myMap);

   //displaying the key-value pairs
   for (auto itr = myMap2.begin(); itr != myMap2.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}
Copy after login

Output

1 One
2 Two
3 Three
Copy after login
Copy after login
Copy after login

in conclusion

Map in C is an ordered set, that is, the elements in the Map are sorted by key value. This makes it slower compared to other similar data structures such as an unordered map where key-value pairs are not sorted. All operations in the map have logarithmic complexity and are implemented in memory as a red-black tree. However, in practice, mapping is very useful because it provides great flexibility in storing data in a key-value manner. We've discussed all the main ways to initialize a map; while there are many ways to initialize, these are the most intuitive ways to do it.

The above is the detailed content of C++ program initialization dictionary. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to write a simple countdown program in C++? How to write a simple countdown program in C++? Nov 03, 2023 pm 01:39 PM

C++ is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to use C++ to write a simple countdown program. The key to implementing a countdown program is to use a timer to calculate the passage of time. In C++, we can use the functions in the time.h header file to implement the timer function. The following is the code for a simple countdown program

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to open a website using Task Scheduler How to open a website using Task Scheduler Oct 02, 2023 pm 11:13 PM

Do you frequently visit the same website at about the same time every day? This can lead to spending a lot of time with multiple browser tabs open and cluttering the browser while performing daily tasks. Well, how about opening it without having to launch the browser manually? It's very simple and doesn't require you to download any third-party apps, as shown below. How do I set up Task Scheduler to open a website? Press the key, type Task Scheduler in the search box, and then click Open. Windows On the right sidebar, click on the Create Basic Task option. In the Name field, enter the name of the website you want to open and click Next. Next, under Triggers, click Time Frequency and click Next. Select how long you want the event to repeat and click Next. Select enable

What to do if the dynamic link library initialization routine fails What to do if the dynamic link library initialization routine fails Dec 29, 2023 am 10:30 AM

Solution: 1. Reinstall the application; 2. Repair or reinstall the DLL; 3. System restore or checkpoint recovery; 4. Scan using System File Checker (SFC); 5. Check startup items and services; 6. Use Tools; 7. Check official documentation or forums; 8. Consider security software; 9. Check the event viewer; 10. Seek expert help, etc.

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

iOS 17: How to organize iMessage apps in Messages iOS 17: How to organize iMessage apps in Messages Sep 18, 2023 pm 05:25 PM

In iOS 17, Apple not only added several new messaging features, but also tweaked the design of the Messages app to give it a cleaner look. All iMessage apps and tools, such as the camera and photo options, can now be accessed by tapping the "+" button above the keyboard and to the left of the text input field. Clicking the "+" button brings up a menu column with a default order of options. Starting from the top, there's camera, photos, stickers, cash (if available), audio, and location. At the very bottom is a "More" button, which when tapped will reveal any other installed messaging apps (you can also swipe up to reveal this hidden list). How to reorganize your iMessage app You can do this below

How to initialize the computer in win7 How to initialize the computer in win7 Jan 07, 2024 am 11:53 AM

The win7 system is a very excellent high-performance system. During the continuous use of win7, many friends are asking how to initialize the computer in win7! Today, the editor will bring you how to restore the factory settings of a win7 computer. Related information on how to initialize the computer in win7: Detailed instructions with pictures and text. Steps: 1. Open the "Start Menu" and enter. 2. Click to enter the settings at the bottom of the left side. 3. In the Win10 update and recovery settings interface, select. 4. Click below "Remove all content and reinstall Windows". 5. You can see the following "Initialization" settings, and then click. 6. Enter the "Your computer has multiple drives" setting option. There are two options here, you can choose according to the situation.

See all articles