. Advertisement .
..3..
. Advertisement .
..4..
In typical cases, programmers may construct a dictionary in C++ with std::maps of the ordinary template library (STL). They may build sorted key-value couples with distinct keys. Anyone will find it a breeze to revise, research, delete and inject the values connected with the keys in the maps under O(N) time complexity.
Nevertheless, only key removals and key additions are two accessible functions. There’s no way to edit them in terms of more intricate facets. To lend a more critical insight, we will cover numerous approaches to the setups of a dictionary. This article also discusses related fundamental attributes.
How to create a dictionary in C++?
Several approaches are possible. Let’s break down two most notable ones.
1. Using Loops
First, build a map whose keys are strings with floated values. Next, assign values to their corresponding keys with the index technique.
From here, we will Loop over the map items via two pointers: the beginning ()
to point to the first elements in the maps and the end ()
that cues the final attributes. In the following example, iter is the iterator.
Some might wonder about the function of *. Let us clarify: the (dereference operator *) operates to retrieve the element to which the pointer refers to.
The first dereference fetches the first variable of a set (i.e., the key). Meanwhile, its second counterpart obtains the secondary value (i.e., value).
Codes:
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
map<string, float> carShowroom;
carShowroom["Mercedes"] = 30;
carShowroom["Audi"] = 18;
carShowroom["Bentley"] = 22;
carShowroom["BMW"] = 32;
carShowroom["Lexus"] = 29;
map <string,float> :: iterator iter;
cout<<"brands"<<" & "<<"amount"<<endl;
for (iter = carShowroom.begin(); iter != carShowroom.end(); iter++)
{
cout<<(*iter).first<<" "<<(*iter).second<<"\n";
}
return 0;}
Output:
brands & amount
Audi 18
BMW 32
Bentley 22
Lexus 29
Mercedes 30
2. Using Default Constructors
Another plausible tactic is to define a map-type object with particular parameters and activate each key-value set using a single line.
The simple illustration below demonstrates a map with integer keys and string values for more comprehensive studies. Still, remember that more intricate real-world scenarios often place these sentences in a loop or employ users’ input.
Codes:
#include <iostream>
#include <map>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::map; using std::copy;
template<typename Map>
void PrintMap(Map& m)
{
cout << "[ ";
for (auto &item : m) {
cout << item.first << ":" << item.second << " ";
}
cout << "]\n";
}
int main() {
map<int, string> cities;
cities[1] = "Moscow";
cities[2] = "London";
cities[3] = "Amsterdam";
cities[4] = "Frankfurt";
cities[5] = "Vancouver";
cout << "cities - ";
PrintMap(cities);
cout << endl;
return EXIT_SUCCESS;
}
Output:
cities - [ 1:Moscow 2:London 3:Amsterdam 4:Frankfurt 5:Vancouver ]
FAQs
1. Is It Necessary to Initialize Map Variables for A Dictionary in C++?
Variable initialization is not just critical for a dictionary. It is, in fact, among the most fundamental concepts to master, particularly for those whose primary programming instrument is the C++ language. Refer to this article for a more inclusive overview.
2. Is It Possible to Set up A Python Dictionary with C++?
Yes, it is feasible. You may create a Python dictionary in the C++ language and transfer them into Python thereafter.
But instead of adopting such a roundabout and time-consuming approach, why don’t you learn how to create a Python dictionary in Python instead? Check this instructional video for more info.[/su_note]
Conclusion
This article has introduced two simple tactics to set up dictionary in C++, with colorful examples to lend you a more critical comprehension. Leave your comments, inquiries, or suggestions in the comment section below!
Leave a comment