Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 61: Using std::unordered_map

std::unordered_map is just an associative container. It works on keys and their maps. Key as the names goes, helps to have uniqueness in the map. While the mapped value is just a content that is associated with the key. The data types of this key and map can be any of the predefined data type or user-defined.

Section 61.1: Declaration and Usage

As already mentioned you can declare an unordered map of any type. Let's have a unordered map named first with string and integer type.

unordered_map<string, int> first; //declaration of the map first["One"] = 1; // [] operator used to insert the value first["Two"] = 2;

first["Three"] = 3; first["Four"] = 4; first["Five"] = 5;

pair <string,int> bar = make_pair("Nine", 9); //make a pair of same type first.insert(bar); //can also use insert to feed the values

Section 61.2: Some Basic Functions

unordered_map<data_type, data_type> variable_name; //declaration variable_name[key_value] = mapped_value; //inserting values variable_name.find(key_value); //returns iterator to the key value variable_name.begin(); // iterator to the first element variable_name.end(); // iterator to the last + 1 element

GoalKicker.com – C++ Notes for Professionals

328