SlideShare a Scribd company logo
1 of 12
Map
Unordered Map
Algorithms
01. int get_number(const string& s)
02. {
03. for (const auto& x : phone_book)
04. if (x.name==s)
05. return x.number;
06. return 0; // use 0 to represent "number not found"
07. }
01. map<string,int> phone_book {
02. {"David Hume",123456},
03. {"Karl Popper",234567},
04. {"Bertrand Arthur William Russell",345678}
05. };
01. int get_number(const string& s)
02. {
03. return phone_book[s];
04. }
01. unordered_map<string,int> phone_book {
02. {"David Hume",123456},
03. {"Karl Popper",234567},
04. {"Bertrand Arthur William Russell",345678}
05. };
01. int get_number(const string& s)
02. {
03. return phone_book[s];
04. }
vector<T> A variable size vector
list<T> A doubly-linked list
forward_list<T> A singly-linked list
deque<T> A doubly-ended queue
set<T> A set
multiset<T> A set in which a value can occur many times
map<K,V> An associative array
multimap<K,V> A map in which a key can occur may times
unordered_map<k,V> A map using a hashed lookup
unordered_multimap<K,V> A multimap using a hashed lookup
unordered_set<T> A set using a hashed lookup
unordered_multiset<T> A multiset using a hashed lookup
 begin() , end()
 push_back()
 size()
01. bool operator<(const Entry& x, const Entry& y)
02. {
03. return x.name<y.name;// order Entrys by their names
04. }
05.
06. void f(vector<Entry>& vec, list<Entry>& lst)
07. {
08. // use < for order
09. sort(vec.begin(),vec.end());
10. // don't copy adjacent equal elements
11. unique_copy(vec.begin(),vec.end(),lst.begin());
12. }
01. // does s contain the character c?
02. bool has_c(const string& s, char c)
03. {
04. auto p = find(s.begin(),s.end(),c);
05. if (p!=s.end())
06. return true;
07. else
08. return false;
09. }
01. bool has_c(const string& s, char c(
02. {
03. return find(s.begin(),s.end(),c)!=s.end();
04. }
p=find(b,e,x) p is the first p in [b:e) so that p==x
p=find_if(b,e,f) p is the first p in [b:e) so that f(p)==true
n=count(b,e,x) n is the number of elemes q in [b:e) so that q==x
n=count_if(b,e,f) n is the number of elems q in [b:e) so that f(q, x)
replace(b,e,v,v2) Replace elements q in [b:e) so that q==v by v2
replace_if(b,e,f,v2) Replace elements q in [b:e) so that f(q) by v2
p=copy(b,e,out) Copy [b:e) to [out:p)
p=copy_if(b,e,out,f) Copy elements q form [b:e) so that f(q) to [out:p)
p=unique_copy(b,e,out) Copy [b:e) to [out:p); don't copy duplicates
sort(b,e) Sort elems of [b:e) using < as the sorting criterion
sort(b,e,f) Sort elems of [b:e) using f as the sorting criterion
(p1,p2)=equal_range(b,e,v) [p1:p2) is the subsequence of the sorted sequence
[b:e) with the value v
p=merge(b,e,b2,e2,out) Merge two sorted sequences [b:e) and [b2:e2)
into [out:p)
15. map, unordered map, algorithms

More Related Content

What's hot

Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium Mihaela Ursachi
 
March 8 Quadratic Equations
March 8 Quadratic EquationsMarch 8 Quadratic Equations
March 8 Quadratic Equationsste ve
 
Pre Calculus notes 4 1 Exponential Functions
Pre Calculus notes 4 1 Exponential FunctionsPre Calculus notes 4 1 Exponential Functions
Pre Calculus notes 4 1 Exponential Functionsmsventrella
 
Lesson 5: Functions and surfaces
Lesson 5: Functions and surfacesLesson 5: Functions and surfaces
Lesson 5: Functions and surfacesMatthew Leingang
 
Quadraticfuntions
QuadraticfuntionsQuadraticfuntions
Quadraticfuntionssuefee
 
6.4 intercept form
6.4 intercept form6.4 intercept form
6.4 intercept formhisema01
 
Pc9 3 polar&rect notes
Pc9 3 polar&rect notesPc9 3 polar&rect notes
Pc9 3 polar&rect notesvhiggins1
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14jchartiersjsd
 
6.4 Graphing Polynomials (Relative Max/Min, Zeros)
6.4 Graphing Polynomials (Relative Max/Min, Zeros)6.4 Graphing Polynomials (Relative Max/Min, Zeros)
6.4 Graphing Polynomials (Relative Max/Min, Zeros)swartzje
 
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...Condição de alinhamento de três pontos,equação geral da reta e área dos triân...
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...MATEMÁTICA PROFISSIONAL
 
5.3 Basic functions. Dynamic slides.
5.3 Basic functions. Dynamic slides.5.3 Basic functions. Dynamic slides.
5.3 Basic functions. Dynamic slides.Jan Plaza
 

What's hot (19)

Polinômios 3
Polinômios 3Polinômios 3
Polinômios 3
 
Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium
 
Exercise #11 notes
Exercise #11 notesExercise #11 notes
Exercise #11 notes
 
March 8 Quadratic Equations
March 8 Quadratic EquationsMarch 8 Quadratic Equations
March 8 Quadratic Equations
 
1 6 Notes
1 6 Notes1 6 Notes
1 6 Notes
 
Unicamp 2017 - aberta
Unicamp 2017 - abertaUnicamp 2017 - aberta
Unicamp 2017 - aberta
 
Pre Calculus notes 4 1 Exponential Functions
Pre Calculus notes 4 1 Exponential FunctionsPre Calculus notes 4 1 Exponential Functions
Pre Calculus notes 4 1 Exponential Functions
 
Lesson 5: Functions and surfaces
Lesson 5: Functions and surfacesLesson 5: Functions and surfaces
Lesson 5: Functions and surfaces
 
Quadraticfuntions
QuadraticfuntionsQuadraticfuntions
Quadraticfuntions
 
6.4 intercept form
6.4 intercept form6.4 intercept form
6.4 intercept form
 
Sistemas lineares 2
Sistemas lineares 2Sistemas lineares 2
Sistemas lineares 2
 
Polinômios 4
Polinômios 4Polinômios 4
Polinômios 4
 
Pc9 3 polar&rect notes
Pc9 3 polar&rect notesPc9 3 polar&rect notes
Pc9 3 polar&rect notes
 
Polinômios 1
Polinômios 1Polinômios 1
Polinômios 1
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14
 
6.4 Graphing Polynomials (Relative Max/Min, Zeros)
6.4 Graphing Polynomials (Relative Max/Min, Zeros)6.4 Graphing Polynomials (Relative Max/Min, Zeros)
6.4 Graphing Polynomials (Relative Max/Min, Zeros)
 
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...Condição de alinhamento de três pontos,equação geral da reta e área dos triân...
Condição de alinhamento de três pontos,equação geral da reta e área dos triân...
 
5.3 Basic functions. Dynamic slides.
5.3 Basic functions. Dynamic slides.5.3 Basic functions. Dynamic slides.
5.3 Basic functions. Dynamic slides.
 
precalc preTest 3
precalc preTest 3precalc preTest 3
precalc preTest 3
 

Similar to 15. map, unordered map, algorithms

14. containers, vector, list
14. containers, vector, list14. containers, vector, list
14. containers, vector, listVahid Heidari
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streamsVahid Heidari
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016Jeff Chu
 
Функциональный микроскоп: линзы в C++
Функциональный микроскоп: линзы в C++Функциональный микроскоп: линзы в C++
Функциональный микроскоп: линзы в C++Platonov Sergey
 
Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Alexander Granin
 
Slide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfSlide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfHimanshuKansal22
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Andrey Akinshin
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsJamesChristianGadian
 
04 meme script
04 meme script04 meme script
04 meme scriptmemeapps
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfezzi97
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointerNishant Munjal
 

Similar to 15. map, unordered map, algorithms (20)

14. containers, vector, list
14. containers, vector, list14. containers, vector, list
14. containers, vector, list
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
 
11. template
11. template11. template
11. template
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016
 
Arrays
ArraysArrays
Arrays
 
Функциональный микроскоп: линзы в C++
Функциональный микроскоп: линзы в C++Функциональный микроскоп: линзы в C++
Функциональный микроскоп: линзы в C++
 
Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Functional microscope - Lenses in C++
Functional microscope - Lenses in C++
 
Slide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfSlide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdf
 
Array
ArrayArray
Array
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
04 meme script
04 meme script04 meme script
04 meme script
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdf
 
Array in C.pdf
Array in C.pdfArray in C.pdf
Array in C.pdf
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
week-7x
week-7xweek-7x
week-7x
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 

More from Vahid Heidari

17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlockVahid Heidari
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introductionVahid Heidari
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchiesVahid Heidari
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containersVahid Heidari
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, errorVahid Heidari
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enumVahid Heidari
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, constsVahid Heidari
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello worldVahid Heidari
 

More from Vahid Heidari (14)

18. utilities
18. utilities18. utilities
18. utilities
 
17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlock
 
16. smart pointers
16. smart pointers16. smart pointers
16. smart pointers
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introduction
 
10. copy and move
10. copy and move10. copy and move
10. copy and move
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containers
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, error
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
 
4. pointers, arrays
4. pointers, arrays4. pointers, arrays
4. pointers, arrays
 
3. tests, loops
3. tests, loops3. tests, loops
3. tests, loops
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, consts
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello world
 

Recently uploaded

Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit MilanNeo4j
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationElement34
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdfSelfMade bd
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Flutter Agency
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringPrakhyath Rai
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaNeo4j
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Lisi Hocke
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfSrushith Repakula
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmuxevmux96
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfICS
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfkalichargn70th171
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 

Recently uploaded (20)

Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmux
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdf
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 

15. map, unordered map, algorithms

  • 1.
  • 3. 01. int get_number(const string& s) 02. { 03. for (const auto& x : phone_book) 04. if (x.name==s) 05. return x.number; 06. return 0; // use 0 to represent "number not found" 07. }
  • 4.
  • 5. 01. map<string,int> phone_book { 02. {"David Hume",123456}, 03. {"Karl Popper",234567}, 04. {"Bertrand Arthur William Russell",345678} 05. }; 01. int get_number(const string& s) 02. { 03. return phone_book[s]; 04. }
  • 6. 01. unordered_map<string,int> phone_book { 02. {"David Hume",123456}, 03. {"Karl Popper",234567}, 04. {"Bertrand Arthur William Russell",345678} 05. }; 01. int get_number(const string& s) 02. { 03. return phone_book[s]; 04. }
  • 7. vector<T> A variable size vector list<T> A doubly-linked list forward_list<T> A singly-linked list deque<T> A doubly-ended queue set<T> A set multiset<T> A set in which a value can occur many times map<K,V> An associative array multimap<K,V> A map in which a key can occur may times unordered_map<k,V> A map using a hashed lookup unordered_multimap<K,V> A multimap using a hashed lookup unordered_set<T> A set using a hashed lookup unordered_multiset<T> A multiset using a hashed lookup
  • 8.  begin() , end()  push_back()  size()
  • 9. 01. bool operator<(const Entry& x, const Entry& y) 02. { 03. return x.name<y.name;// order Entrys by their names 04. } 05. 06. void f(vector<Entry>& vec, list<Entry>& lst) 07. { 08. // use < for order 09. sort(vec.begin(),vec.end()); 10. // don't copy adjacent equal elements 11. unique_copy(vec.begin(),vec.end(),lst.begin()); 12. }
  • 10. 01. // does s contain the character c? 02. bool has_c(const string& s, char c) 03. { 04. auto p = find(s.begin(),s.end(),c); 05. if (p!=s.end()) 06. return true; 07. else 08. return false; 09. } 01. bool has_c(const string& s, char c( 02. { 03. return find(s.begin(),s.end(),c)!=s.end(); 04. }
  • 11. p=find(b,e,x) p is the first p in [b:e) so that p==x p=find_if(b,e,f) p is the first p in [b:e) so that f(p)==true n=count(b,e,x) n is the number of elemes q in [b:e) so that q==x n=count_if(b,e,f) n is the number of elems q in [b:e) so that f(q, x) replace(b,e,v,v2) Replace elements q in [b:e) so that q==v by v2 replace_if(b,e,f,v2) Replace elements q in [b:e) so that f(q) by v2 p=copy(b,e,out) Copy [b:e) to [out:p) p=copy_if(b,e,out,f) Copy elements q form [b:e) so that f(q) to [out:p) p=unique_copy(b,e,out) Copy [b:e) to [out:p); don't copy duplicates sort(b,e) Sort elems of [b:e) using < as the sorting criterion sort(b,e,f) Sort elems of [b:e) using f as the sorting criterion (p1,p2)=equal_range(b,e,v) [p1:p2) is the subsequence of the sorted sequence [b:e) with the value v p=merge(b,e,b2,e2,out) Merge two sorted sequences [b:e) and [b2:e2) into [out:p)

Editor's Notes

  1. برای معرفی map با بازم به سراغ مثال دفترچه تلفن می ریم که توی ویدیوهای قبلی بحث کردیم. یکی از کار ها متداول توی دفترچه تلفن اینه که ما اسم کسی رو توی دفترچه تلفن search کنیم و شماره اونو پیدا کنیم. اگه دفترچه تلفن با لیست یا وکتور پیاده سازی شده باشه، باید به صورت خطی از اول تا آخر لیست رو دنبال رکورد مورد نظرمون بگردیم ، چون تمام المانها پشت سر هم هستند ویا انیکه به صورت پشت سر هم باید بهشون دسترسی داشته باشیم. این روش search کردن خیلی پر هزینه ست مخصوصا زمانی که تعداد entryها زیاد باشه.
  2. کتابخونه استاندارد یه ساختمان داده به نام map داره که برای search کردن امکانات خوبی ارائه میده. Map با استفاده از red-black tree پیاده سازی شده که یه نوع binary search treeی بالانس شده است و مرتبه ی زمانی search توش خیلی از لیست بهتره. ما به map میگیم associative array و اسم دیگه ی اون Dictionary هم هست. المانهای Map به صورت Pairیی از key value یا کلید و مقدار هست که برای جستجو بهینه سازی شده.
  3. Initializeکردن map مثل vector و list هست و میشه اونو با initializer-list مقدار دهی کرد. می تونیم با آرگمان اول که بهش می گیم Key یه map رو index کرد و مقدار value رو گرفت. با اپراتور subscript در واقع ما داریم یه search داخل map میکنیم. اگه کلیدی که ما داریم سرچ میکنیم داخل map نباشه یه رکورد توی map ایجاد میشه و value اون با مقدار default پر میشه. اگر بخوایم جلوی پر شدن map رو با مقادیر Default که معمولا غیر معتبر هستند رو بگیریم باید بجای subscript از متد find استفاده کنیم. متد find اگه کلید مورد نظر ما داخل map نباشه یه iterator به انتهای map بر می گردونه.
  4. هزینه ی Search توی map از مرتبه ی O(log(n)) هست و n تعداد المانهای داخل Map هست. مرتبه ی زمانی map خیلی خوبه مثلا فرض کنید که 1 میلیون رکورد توی map باشه ما می تونیم با حدود 20تا مقایسه به عنصری که دنبالشیم دسترسی داشته باشیم. اما ساختمان داده ای مثل hash هم وجود داره که ما می تونیم بدون مقایسه به عنصر مورد نظر برسیم. توی C++ به hash table ها unordered میگن چون توی hash معمولا ترتیب مهم نیست. یکی از اون hash table ها unordered_map هست. با initializer list میشه unordered_map رو مقدار دهی کرد. برای دسترسی به المانها اون هم میشه مشابه map از subscript استفاده کرد. کتابخونه استاندارد برای string یه hash function پیش فرض داره که اگه بخواید می تونید اونو با یه تابع دیگه جایگزین کرد.
  5. توی این اسلاید یه Overview از تمام Containerهایی که کتابخونه استاندارد داره رو می بینید. Containerهایی که با unordered شروع میشن برای lookup بهینه سازی شدن و با یه کلید که معمولا string هست یه value رو ذخیره می کنن. پیاده سازی اونا هم با hash table هست. تمامی containerها در namespace ی std قرار دارند. علاوه بر اینا کتابخونه استاندارد containerهایی مثل stack، queue، deque و priority_queue رو هم پشتیبانی میکنه. همین طور bitset و آرایه ی با طول ثابت یا همون Array رو هم فراهم کرده. تمامی Containerها و Operationهاشون به صورتی طراحی شدن که از طرف کاربر ظاهر یکسانی داشته باشند. تمامی operationهایی که اسمشون یکی هست معنی یکسانی هم دارند و کار یکسانی انجام میدن.
  6. برای مثال تمامی Containerها دارای توابعی به نام های begin و end هستند که با begin اولین المان داخل Container برگردونده میشه و با end المان یکی بعد از آخرین المان داخل Container برگردونده میشه. تابع push_back یه المان رو به انتهای vector ، list و forward list و بقیه ی containerها اضافه میکنه. تابع size تعداد المان هایی که داخل container هستند رو بر می گردونه. وقتی notation و semantic کتابخونه استاندارد یکسان باشه برنامه نویس ها می تون containerهای جدید رو که در کتابخونه نیست اضافه کنن و از الگوریتم های موجود هم روی Containerهایی که اضافه کردن استفاده کنن.
  7. ساختمان داده هایی مثل vector و list به خودی خود سودمند نیستند. برای استفاده از اونا ما نیاز داریم تا یه سری عملیاتهای basic مثل دسترسی به المان ها و اضافه و حذف کردن المانها رو داشته باشیم. اگه فقط بخوایم یه سری object داخل اونا ذخیره کنیم زیاد مفید نیست. ما به عملیاتهایی مثل مرتب سازی، چاپ کردن المانها، گرفتن یه زیرمجموعه از container، search ، remove و غیره نیاز داریم. به همین خاطر کتابخونه استاندارد الگوریتم های رایج روی container ها رو فراهم کرده. مثال زیر یه وکتور رو sort میکنه و بعد به صورت unique المانهای وکتور رو توی لیست کپی میکنه. در این مثال ما با استفاده از iteratorهای begin و end ابتدا و انتهای یه sequence از المان هایی رو که می خوایم sort کنیم رو مشخص می کنیم. توابع sort و unique_copy دو تا از الگوریتم هایی هستند که کتابخونه استاندارد برای ما فراهم کرده.
  8. بیشتر الگوریتم ها با iteratorکار میکنند و نتیجه ی خروجیشون رو به صورت iterator برمیگردون. مثلا find دنبال یه عنصر داخل sequence میگرده و اگه اونو پیدا بکنه یه Iterator به اون بر می گردونه. اگر المانی که دنبالش می گردیم رو پیدا نکنه، iterator به end رو بر می گردونه. نسخه ی کوتاه شده ی کدهای بالا رو میبینید که کل کار های کدهای بالا رو در یک خط انجام میده. نکته اینه که اگه شما بتونید تعداد خط کدها رو کم کنید ولی همچنان کدها خوانا و قابل فهم باشن بعدا موقعی که می خواید کدها رو maintain یا refactor کنید مشکلات کمتری خواهید داشت.
  9. توی این اسلاید یه overview از الگوریتم های کتابخونه استاندارد رو می بینیم. کتابخونه استاندارد تعداد زیادی الگوریتم های general و پر کاربرد فراهم کرده. همه ی الگوریتم ها در namespaceی std هستند و با include کردنheader file ی algorithm در دسترس قرار می گیرند.
  10. جمع بندی فصل چهارم در آخر ویدیوی 15 ما فصل چهارم کتاب رو تموم کردیم. توی فصل چهارم یه معرفی اجمالی در مورد امکانات کتابخونه استاندارد داشتیم و با string، IO stream ، Containerها و الگوریتم ها آشنا شدیم.