unordered_map es un contenedor asociado que almacena elementos formados por la combinación de clave-valor y un valor mapeado. El valor de la clave se utiliza para identificar de forma única el elemento y el valor asignado es el contenido asociado con la clave. Tanto la clave como el valor pueden ser de cualquier tipo predefinido o definido por el usuario.
Unordered_map internamente se implementa utilizando Hash Table , la clave proporcionada para mapear se convierte en índices de una tabla hash, por eso el rendimiento de la estructura de datos depende mucho de la función hash, pero en promedio, el costo de buscar, insertar y eliminar de la tabla hash es O(1).
C++
// C++ program to demonstrate functionality of unordered_map #include <iostream> #include <unordered_map> using namespace std; int main() { // Declaring umap to be of <string, int> type // key will be of string type and mapped value will // be of int type unordered_map<string, int> umap; // inserting values by using [] operator umap["GeeksforGeeks"] = 10; umap["Practice"] = 20; umap["Contribute"] = 30; // Traversing an unordered map for (auto x : umap) cout << x.first << " " << x.second << endl; }
C++
// C++ program to demonstrate functionality of unordered_map #include <iostream> #include <unordered_map> using namespace std; int main() { // Declaring umap to be of <string, double> type // key will be of string type and mapped value will // be of double type unordered_map<string, double> umap; // inserting values by using [] operator umap["PI"] = 3.14; umap["root2"] = 1.414; umap["root3"] = 1.732; umap["log10"] = 2.302; umap["loge"] = 1.0; // inserting value by insert function umap.insert(make_pair("e", 2.718)); string key = "PI"; // If key not found in map iterator to end is returned if (umap.find(key) == umap.end()) cout << key << " not found\n\n"; // If key found then iterator to that key is returned else cout << "Found " << key << "\n\n"; key = "lambda"; if (umap.find(key) == umap.end()) cout << key << " not found\n"; else cout << "Found " << key << endl; // iterating over all value of umap unordered_map<string, double>:: iterator itr; cout << "\nAll Elements : \n"; for (itr = umap.begin(); itr != umap.end(); itr++) { // itr works as a pointer to pair<string, double> // type itr->first stores the key part and // itr->second stores the value part cout << itr->first << " " << itr->second << endl; } }
C++
// C++ program to find freq of every word using // unordered_map #include <bits/stdc++.h> using namespace std; // Prints frequencies of individual words in str void printFrequencies(const string &str) { // declaring map of <string, int> type, each word // is mapped to its frequency unordered_map<string, int> wordFreq; // breaking input into word using string stream stringstream ss(str); // Used for breaking words string word; // To store individual words while (ss >> word) wordFreq[word]++; // now iterating over word, freq pair and printing // them in <, > format unordered_map<string, int>:: iterator p; for (p = wordFreq.begin(); p != wordFreq.end(); p++) cout << "(" << p->first << ", " << p->second << ")\n"; } // Driver code int main() { string str = "geeks for geeks geeks quiz " "practice qa for"; printFrequencies(str); return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA