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 (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

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...Andreas Dewes
 
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)Maik Becker
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxVeerannaKotagi1
 
Chart clicker presentation
Chart clicker presentationChart clicker presentation
Chart clicker presentationdwm042
 
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 modelDebasish Ghosh
 
Deep dive into deeplearn.js
Deep dive into deeplearn.jsDeep dive into deeplearn.js
Deep dive into deeplearn.jsKai Sasaki
 
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.pdfrishteygallery
 
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 .docxKomlin1
 
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.pdfrahulfancycorner21
 
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.pdfgiriraj65
 
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 : NotesSubhajit Sahu
 
Basic Deep Learning.pptx
Basic Deep Learning.pptxBasic Deep Learning.pptx
Basic Deep Learning.pptxmabog44
 
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.docxIsaac9LjWelchq
 

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

Part 5 examination of reflex
Part 5 examination of reflexPart 5 examination of reflex
Part 5 examination of reflexAtul Saswat
 
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 systemAtul Saswat
 
Part 3 cranial nerve examination
Part 3 cranial nerve examinationPart 3 cranial nerve examination
Part 3 cranial nerve examinationAtul Saswat
 
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 patientAtul Saswat
 
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 examinationAtul Saswat
 

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

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 

Recently uploaded (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 

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