SlideShare a Scribd company logo
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

Polinômios 3
Polinômios 3Polinômios 3
Polinômios 3
KalculosOnline
 
Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium Interesting Mathematics6- Belgium
Interesting Mathematics6- Belgium
Mihaela Ursachi
 
Exercise #11 notes
Exercise #11 notesExercise #11 notes
Exercise #11 notes
Kelly Scallion
 
March 8 Quadratic Equations
March 8 Quadratic EquationsMarch 8 Quadratic Equations
March 8 Quadratic Equations
ste ve
 
1 6 Notes
1 6 Notes1 6 Notes
Unicamp 2017 - aberta
Unicamp 2017 - abertaUnicamp 2017 - aberta
Unicamp 2017 - aberta
KalculosOnline
 
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
msventrella
 
Lesson 5: Functions and surfaces
Lesson 5: Functions and surfacesLesson 5: Functions and surfaces
Lesson 5: Functions and surfaces
Matthew Leingang
 
Quadraticfuntions
QuadraticfuntionsQuadraticfuntions
Quadraticfuntions
suefee
 
6.4 intercept form
6.4 intercept form6.4 intercept form
6.4 intercept form
hisema01
 
Sistemas lineares 2
Sistemas lineares 2Sistemas lineares 2
Sistemas lineares 2
KalculosOnline
 
Polinômios 4
Polinômios 4Polinômios 4
Polinômios 4
KalculosOnline
 
Pc9 3 polar&rect notes
Pc9 3 polar&rect notesPc9 3 polar&rect notes
Pc9 3 polar&rect notes
vhiggins1
 
Polinômios 1
Polinômios 1Polinômios 1
Polinômios 1
KalculosOnline
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14
jchartiersjsd
 
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
 
precalc preTest 3
precalc preTest 3precalc preTest 3
precalc preTest 3
A Jorge Garcia
 

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, list
Vahid Heidari
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
Vahid Heidari
 
11. template
11. template11. template
11. template
Vahid Heidari
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016
Jeff Chu
 
Arrays
ArraysArrays
Функциональный микроскоп: линзы в 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.pdf
HimanshuKansal22
 
Array
ArrayArray
Array
Radha Rani
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Что нам готовит грядущий 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 Lessons
JamesChristianGadian
 
04 meme script
04 meme script04 meme script
04 meme script
memeapps
 
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
ezzi97
 
Array in C.pdf
Array in C.pdfArray in C.pdf
Array in C.pdf
georgejustymirobi1
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
mounikanarra3
 
Array.pptx
Array.pptxArray.pptx
week-7x
week-7xweek-7x
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
NehaJain919374
 

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

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

8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 

Recently uploaded (20)

8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 

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ها و الگوریتم ها آشنا شدیم.