SlideShare a Scribd company logo
1 of 3
Download to read offline
Hash Tables
June 19, 2020
0.1 Dictionary Example in Code
In the cell below, we have created a hash table (unordered_map) to store the data from the example
above. To create an unordered_map in C++, you must include the <unordered_map> header, and
the sytnax for declaring an unordered_map is as follows:
unordered_map <key_type, value_type> variable_name;
In the code below, we check if the key is in the unordered_map using the .find() method. If the
key does not exist in the map, then .find() returns an unordered_map::end() type. Otherwise,
.find() returns a C++ iterator, which is a pointer that points to the beginning of the iterable
key-value pair.
We haven’t covered iterators in this course, and you won’t need them for this project, but they
are a lot like pointers that can "iterate" forward or backward through a range.
In [ ]: # include <iostream>
# include <vector>
# include <unordered_map>
# include <string>
using std::vector;
using std::cout;
using std::unordered_map;
using std::string;
int main() {
// Create strings to use in the hash table.
string key = "word";
string def_1 = "a unit of language, consisting of one or more spoken sounds or their
string def_2 = "speech or talk: to express ones emotion in words;
string def_3 = a short talk or conversation: Marston, Id like a word with you.;
string def_4 = an expression or utterance: a word of warning;
unordered_map string, vectorstring my_dictionary;
// Check if key is in the hash table.
if (my_dictionary.find(key) == my_dictionary.end()) {
cout  The key word is not in the dictionary.  n;
cout  Inserting a key-value pair into the dictionary.  nn;
1
// Set the value for the key.
my_dictionary[key] = vectorstring {def_1, def_2, def_3, def_4};
}
// The key should now be in the hash table. You can access the
// value corresponding to the key with square brackets [].
// Here, the value my_dictionary[key] is a vector of strings.
// We iterate over the vector and print the strings.
cout  key  : n;
auto definitions = my_dictionary[key];
for (string definition : definitions) {
cout  definition  n;
}
}
Compile  Execute
Explain
Loading terminal (id_sxuzijq), please wait...
0.2 Your Turn!
The following is data relating some international country phone codes to their corresponding
country name:
{{972, Israel}, {93, Afghanistan}, {355, Albania}, {213, Algeria}, {376, Andorra}, {24
You can create an unordered_map with the data above just by pasting it into your program
and assigning it to the appropriate variable. For example, an unordered_map can be declared and
initialized as follows:
std::unordered_mapint, std::string mymap {
{5, a},
{6, b},
{7, c}
};
Instructions
• Write a C++ program that creates an unordered_map to store the data above.
• Write some code to test if the 960 country code is in the data. If it is not, add the key/pair
{960, Maldives} to the unordered_map.
• Print out the value associated with the key 960 once you have done that to check that it is
there.
In [ ]: # include iostream
# include vector
# include unordered_map
# include string
2
using std::vector;
using std::cout;
using std::unordered_map;
using std::string;
int main()
{
unordered_map int, string callbook = {{972, Israel}, {93, Afghanistan}, {355,
if(callbook.find(960) == callbook.end())
{
callbook[960] = Maldives;
}
cout  key:   960  n;
cout  value:   callbook[960]  n;
vector my_codes = {972,93,355,960};
for(int i:my_codes)
{
cout  i  n;
cout  callbook[i]  n;
}
return 0;
}
Compile  Execute
See Solution
Loading terminal (id_8z9gwjg), please wait...
3

More Related Content

What's hot

JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
OXUS 20
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
Hadziq Fabroyir
 

What's hot (20)

Sigma type
Sigma typeSigma type
Sigma type
 
Function basics
Function basicsFunction basics
Function basics
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 
C++ programming structure & union
C++ programming structure & unionC++ programming structure & union
C++ programming structure & union
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Codejunk Ignitesd
Codejunk IgnitesdCodejunk Ignitesd
Codejunk Ignitesd
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
CS50 Lecture4
CS50 Lecture4CS50 Lecture4
CS50 Lecture4
 
Ecs40 winter 2017 homework 3
Ecs40 winter 2017 homework 3Ecs40 winter 2017 homework 3
Ecs40 winter 2017 homework 3
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
L10
L10L10
L10
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 

Similar to Hash tables

Chart clicker presentation
Chart clicker presentationChart clicker presentation
Chart clicker presentation
dwm042
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdf
rishteygallery
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
rahulfancycorner21
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdf
giriraj65
 
hello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docxhello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docx
Isaac9LjWelchq
 

Similar to Hash tables (20)

Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docx
 
Chart clicker presentation
Chart clicker presentationChart clicker presentation
Chart clicker presentation
 
Internal workshop es6_2015
Internal workshop es6_2015Internal workshop es6_2015
Internal workshop es6_2015
 
DSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic modelDSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic model
 
Deep dive into deeplearn.js
Deep dive into deeplearn.jsDeep dive into deeplearn.js
Deep dive into deeplearn.js
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdf
 
This code currently works... Run it and get a screen shot of its .docx
 This code currently works... Run it and get a screen shot of its .docx This code currently works... Run it and get a screen shot of its .docx
This code currently works... Run it and get a screen shot of its .docx
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
 
COLLADA & WebGL
COLLADA & WebGLCOLLADA & WebGL
COLLADA & WebGL
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdf
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
 
Basic Deep Learning.pptx
Basic Deep Learning.pptxBasic Deep Learning.pptx
Basic Deep Learning.pptx
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
hello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docxhello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docx
 
5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt
 
OpenGL L06-Performance
OpenGL L06-PerformanceOpenGL L06-Performance
OpenGL L06-Performance
 
C++
C++C++
C++
 

More from Atul Saswat (6)

VSSUT 65
VSSUT 65VSSUT 65
VSSUT 65
 
Part 5 examination of reflex
Part 5 examination of reflexPart 5 examination of reflex
Part 5 examination of reflex
 
Part 4 examination of motor and sensory system
Part 4 examination of motor and sensory systemPart 4 examination of motor and sensory system
Part 4 examination of motor and sensory system
 
Part 3 cranial nerve examination
Part 3 cranial nerve examinationPart 3 cranial nerve examination
Part 3 cranial nerve examination
 
Part 1 function of brain and history taking of a neurological patient
Part 1 function of brain and history taking of a neurological patientPart 1 function of brain and history taking of a neurological patient
Part 1 function of brain and history taking of a neurological patient
 
Part 2 general physical and mental examination
Part 2 general physical and mental examinationPart 2 general physical and mental examination
Part 2 general physical and mental examination
 

Recently uploaded

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Hash tables

  • 1. Hash Tables June 19, 2020 0.1 Dictionary Example in Code In the cell below, we have created a hash table (unordered_map) to store the data from the example above. To create an unordered_map in C++, you must include the <unordered_map> header, and the sytnax for declaring an unordered_map is as follows: unordered_map <key_type, value_type> variable_name; In the code below, we check if the key is in the unordered_map using the .find() method. If the key does not exist in the map, then .find() returns an unordered_map::end() type. Otherwise, .find() returns a C++ iterator, which is a pointer that points to the beginning of the iterable key-value pair. We haven’t covered iterators in this course, and you won’t need them for this project, but they are a lot like pointers that can "iterate" forward or backward through a range. In [ ]: # include <iostream> # include <vector> # include <unordered_map> # include <string> using std::vector; using std::cout; using std::unordered_map; using std::string; int main() { // Create strings to use in the hash table. string key = "word"; string def_1 = "a unit of language, consisting of one or more spoken sounds or their string def_2 = "speech or talk: to express ones emotion in words; string def_3 = a short talk or conversation: Marston, Id like a word with you.; string def_4 = an expression or utterance: a word of warning; unordered_map string, vectorstring my_dictionary; // Check if key is in the hash table. if (my_dictionary.find(key) == my_dictionary.end()) { cout The key word is not in the dictionary. n; cout Inserting a key-value pair into the dictionary. nn; 1
  • 2. // Set the value for the key. my_dictionary[key] = vectorstring {def_1, def_2, def_3, def_4}; } // The key should now be in the hash table. You can access the // value corresponding to the key with square brackets []. // Here, the value my_dictionary[key] is a vector of strings. // We iterate over the vector and print the strings. cout key : n; auto definitions = my_dictionary[key]; for (string definition : definitions) { cout definition n; } } Compile Execute Explain Loading terminal (id_sxuzijq), please wait... 0.2 Your Turn! The following is data relating some international country phone codes to their corresponding country name: {{972, Israel}, {93, Afghanistan}, {355, Albania}, {213, Algeria}, {376, Andorra}, {24 You can create an unordered_map with the data above just by pasting it into your program and assigning it to the appropriate variable. For example, an unordered_map can be declared and initialized as follows: std::unordered_mapint, std::string mymap { {5, a}, {6, b}, {7, c} }; Instructions • Write a C++ program that creates an unordered_map to store the data above. • Write some code to test if the 960 country code is in the data. If it is not, add the key/pair {960, Maldives} to the unordered_map. • Print out the value associated with the key 960 once you have done that to check that it is there. In [ ]: # include iostream # include vector # include unordered_map # include string 2
  • 3. using std::vector; using std::cout; using std::unordered_map; using std::string; int main() { unordered_map int, string callbook = {{972, Israel}, {93, Afghanistan}, {355, if(callbook.find(960) == callbook.end()) { callbook[960] = Maldives; } cout key: 960 n; cout value: callbook[960] n; vector my_codes = {972,93,355,960}; for(int i:my_codes) { cout i n; cout callbook[i] n; } return 0; } Compile Execute See Solution Loading terminal (id_8z9gwjg), please wait... 3