SlideShare a Scribd company logo
Containers
Vector
List
 Collections of values
 Manipulating such collections
 A class with the main purpose of holding
objects is commonly called a container
01. vector<Entry> phone_book = {
02. {"David Hume",123456},
03. {"Karl Popper",234567},
04. {"Bertrand Arthur William Russell",345678}
05. };
01. void print_book(const vector<Entry>& book)
02. {
03. for (int i = 0; i!=book.size(); ++i)
04. cout << book[i] << 'n';
05. }
01. void print_book(const vector<Entry>& book)
02. {
03. for (const auto& x : book) // for "auto" see §2.2.2
04. cout << x << 'n';
05. }
01. vector<int> v1 = {1, 2, 3, 4}; // size is 4
02. vector<string> v2; // size is 0
03. // size is 23; initial element value: nullptr
04. vector<Shape*> v3(23);
05. // size is 32; initial element value: 9.9
06. vector<double> v4(32,9.9);
01. void input()
02. {
03. for (Entry e; cin>>e;)
04. phone_book.push_back(e);
05. }
01. vector<Entry> book2 = phone_book;
 vector<T>
 When you insert a new element, it’s value is
copied into the container
01. void silly(vector<Entry>& book)
02. {
03. // book.size() is out of range
04. int i = book[ph.size()].number;
05. // ...
06. }
01. template<typename T>
02. class Vec : public std::vector<T> {
03. public:
04. // use the constructors from vector (under the name Vec)
05. using vector<T>::vector;
06. T& operator[](int i) // range check
07. { return vector<T>::at(i); }
08. const T& operator[](int i) const // range check const objects
09. { return vector<T>::at(i); }
10. }
01. list<Entry> 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. 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. int get_number(const string& s)
02. {
03. for (auto p = phone_book.begin(); p!=phone_book.end(); ++p)
04. if (p–>name==s)
05. return p–>number;
06. return 0; // use 0 to represent "number not found"
07. }
01. int get_number(const string& s)
02. {
03. for (auto p = phone_book.begin(); p!=phone_book.end(); ++p)
04. if (p–>name==s)
05. return p–>number;
06. return 0; // use 0 to represent "number not found"
07. }
01. void f(const Entry& ee, list<Entry>::iterator p
02. , list<Entry>::iterator q)
03. {
04. // add ee before the element referred to by p
05. phone_book.insert(p, ee);
06. // remove the element referred to by q
07. phone_book.erase(q);
08. }
14. containers, vector, list

More Related Content

Similar to 14. containers, vector, list

15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithms
Vahid Heidari
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
Vahid Heidari
 
11. template
11. template11. template
11. template
Vahid Heidari
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
Tekle12
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
GlobalLogic Ukraine
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
Jawad Khan
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
Ilio Catallo
 
10. copy and move
10. copy and move10. copy and move
10. copy and move
Vahid Heidari
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
suchitrapoojari984
 

Similar to 14. containers, vector, list (12)

15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithms
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
 
11. template
11. template11. template
11. template
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
10. copy and move
10. copy and move10. copy and move
10. copy and move
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.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
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
Vahid Heidari
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introduction
Vahid Heidari
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
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 (13)

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
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introduction
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
 
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

Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
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
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
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
 
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
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
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
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
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
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
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
 

Recently uploaded (20)

Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
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
 
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
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
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
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
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
 

14. containers, vector, list

  • 1.
  • 3.  Collections of values  Manipulating such collections  A class with the main purpose of holding objects is commonly called a container
  • 4. 01. vector<Entry> phone_book = { 02. {"David Hume",123456}, 03. {"Karl Popper",234567}, 04. {"Bertrand Arthur William Russell",345678} 05. }; 01. void print_book(const vector<Entry>& book) 02. { 03. for (int i = 0; i!=book.size(); ++i) 04. cout << book[i] << 'n'; 05. }
  • 5. 01. void print_book(const vector<Entry>& book) 02. { 03. for (const auto& x : book) // for "auto" see §2.2.2 04. cout << x << 'n'; 05. } 01. vector<int> v1 = {1, 2, 3, 4}; // size is 4 02. vector<string> v2; // size is 0 03. // size is 23; initial element value: nullptr 04. vector<Shape*> v3(23); 05. // size is 32; initial element value: 9.9 06. vector<double> v4(32,9.9);
  • 6. 01. void input() 02. { 03. for (Entry e; cin>>e;) 04. phone_book.push_back(e); 05. } 01. vector<Entry> book2 = phone_book;
  • 7.  vector<T>  When you insert a new element, it’s value is copied into the container
  • 8. 01. void silly(vector<Entry>& book) 02. { 03. // book.size() is out of range 04. int i = book[ph.size()].number; 05. // ... 06. } 01. template<typename T> 02. class Vec : public std::vector<T> { 03. public: 04. // use the constructors from vector (under the name Vec) 05. using vector<T>::vector; 06. T& operator[](int i) // range check 07. { return vector<T>::at(i); } 08. const T& operator[](int i) const // range check const objects 09. { return vector<T>::at(i); } 10. }
  • 9. 01. list<Entry> 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. 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. }
  • 10. 01. int get_number(const string& s) 02. { 03. for (auto p = phone_book.begin(); p!=phone_book.end(); ++p) 04. if (p–>name==s) 05. return p–>number; 06. return 0; // use 0 to represent "number not found" 07. }
  • 11. 01. int get_number(const string& s) 02. { 03. for (auto p = phone_book.begin(); p!=phone_book.end(); ++p) 04. if (p–>name==s) 05. return p–>number; 06. return 0; // use 0 to represent "number not found" 07. } 01. void f(const Entry& ee, list<Entry>::iterator p 02. , list<Entry>::iterator q) 03. { 04. // add ee before the element referred to by p 05. phone_book.insert(p, ee); 06. // remove the element referred to by q 07. phone_book.erase(q); 08. }

Editor's Notes

  1. توی این دوباره یه اشاره ای به مفهوم container می کنم و ویدیو وکتور و لیست رو که دوتا از پر کاربرد ترین containerهای کتابخونه استاندارد رو معرفی میکنم.
  2. برنامه هایی که می نویسیم معمولا شامل یه مجموعه از مقادیری مشن که ما اونا رو ایجاد می کنیم و یه collection از اونا رو درست می کنیم و بعد روی اونا عملیاتهایی رو انجام می دیم. مثال کار کردن با string و خوندن کاراکتر ها از روی ورودی و چاپ کردن مقدار اون یه مثال ساده هست. در ویدیوی شماره 7 در مورد مفهوم containerها صحبت کرده بودیم. هر کلاسی که یه مجموعه از Objectها رو نگه داری کنه بهش می گیم Container. برای اینکه یه کاری رو انجام بدیم باید container مناسب اون کار رو به همراه عملیاتهای پایه ای روی Container رو باید داشته باشیم. برای اینکه روشن تر بشه فرض کنید که ما می خوایم یه دفترچه تلفن درست کنیم که شامل یه سری نام و شماره تلفن اون نام هست. ما باید طوری این برنامه رو بنویسیم که واضح و خوانا باشه و هر کسی با هر پیش زمینه ای بتونه از اون سر در بیاره. Structی Entry که در ویدیوی قبلی دیدیم می تونه برای نگه داری یه رکورد توی دفترچه تلفن استفاده بشه. توی اون struct خیلی از پیچیدگی های دنیای واقعی رو در نظر نمی گیریم مثلا تلفن های واقعی توی int جا نمی شن ولی برای سادگی ما شماره تلفن رو int در نظر گرفتیم. مثال دفترچه تلفن توی تمام این فصل برای توضیح دادن containerها استفاده می شه.
  3. مفیدترین container در کتابخونه استاندارد وکتوره. وکتور یه sequence از المانهاست که پشت سر هم و به صورت پیوسته در حافظه قرار گرفتند. در مورد وکتور تا حالا خیلی بحث کردیم و مثال زدیم. ما میتونیم وکتور را با یه set از eleman typeهایی که داره initialize کنیم . توی این مثال یه وکتور از entryها به نام phone_book ساختیم و با 3 تا عنصر مقدار دهی کردیم. با اپراتور subscript به عناصر اون دسترسی داشته باشیم. Indexها از صفر شروع میشن یعنی book[0] برابر رکورد david hume خواهد بود. وکتور یه تابع به نام size داره که تعداد المانهای داخل اون رو بر میگردونه. ما از size توی for برای شرط خروج حلقه استفاده کردیم.
  4. ما می تونیم از range for برای دسترسی به المانهای وکتور استفاده کنیم. با range for به تک تک عناصر وکتور دسترسی داریم و می تونیم عملیات انجام بدیم. با استفاده از auto می تونیم type المانهای وکتور رو ننویسیم و کامپایلر خودش به صورت هوشمند این کار رو می کنه. هر وقت یه وکتور تعریف می کنیم size اون بر اساس المانهایی که برای initializeکردن بهش می دیم مقدار می گیره. اینجا چون 4 تا المان به عنوان initializer list به وکتور دادیم پس سایز اولیه اون برابر 4 میشه. اما اینجا وکتور initialize نشده پس مقدار size برابر صفر خواهد شد. این خط با استفاده از کانستراکتور تک ورودی یه وکتور با 23 تا المان میسازه که مقادیر اولیه اون المانها با nullptr مقدار دهی می شن. اگر برای المانهای وکتور مقدار اولیه تعیین نشه default constructor اون المانها اجرا خواهد شد. و اینجا با کانستراکتور 2 ورودی 32 تا المان میسازه که با 9.9 مقدار اولیه داده میشن. مقدار اولیه default رو با دادن پارامتر دوم به وکتور میشه تغییر دار و بجای اینکه default constructor اجرا بشه با این پارامتر مقدار دهی میشه.
  5. یکی از فیچر های مهم وکتور اینه که بر خلاف آرایه که سایزش غیرقابل تغییره ، تعداد عناصر داخل vector رو میشه تغییر داد. با تابع push_back میشه یه المان رو به انتهای وکتور اضافه کرد. تابع input از ورودی یه تعداد entry میخونه و بعد اونارو به انتهای وکتور اضافه میکنه. زمانی این حلقه تموم میشه که به انتهای ورودی برسیم و یا فرمت بندی ورودی اشتباه باشه. Vector طوری پیاده سازی شده که با push_back کردن المانهای جدید، رشد میکنه و بزرگ میشه تا جا برای المانهای جدید هم باز بشه. این بزرگ شدن وکتور رو طوری پیاده سازی کردند که تا حد قابل قبولی efficient باشه. یه وکتور جدید رو میشه با assignment , initialize کرد. چون copy و move constructor برای وکتور تعریف شده و میشه با Move و Copy کردن وکتور رو initialize کرد. ولی زمانی که وکتور تعداد زیادی المان داخلش باشه copy هزینه ی زیادی داره و باید از move کمک گرفت یا اینکه از pointer یا reference استفاده کرد.
  6. توی این اسلاید در مورد چند تا نکته مهم در مورد المانهای وکتور اشاره میکنم. وکتور و همه ی containerها کتابخونه استاندارد به صورت templateیی پیاده سازی شده و type المان های اون پارامتری تعیین میشه. پارامتر T میتونه هر چیزی مثل built-in typeها یا Pointerیا user-defined type ها باشه. وقتی یه المان جدید به وکتور push_back میشه value اون copyمیشه. برای المان های کوچیک مثل اعداد صحیح این کپی کردن هزینه ای نداره ولی اگه اندازه المان ها بزرگتر بشه هزینه ی کپی کردن بالا میره. این نکته ایه که وقتی از وکتور استفاده میکنید باید مد نظر داشته باشید.
  7. یه نکته هم در مورد اپراتور subscript توی این اسلاید میگم و اون اینکه وکتور هیچ تضمینی برای چک کردن دسترسی های out of range یا خارج از محدوده نداره. برای اینکه این مشکل حل بشه میتونیم یه کلاس به این صورت بنویسم که از وکتور مشتق شده باشه و اپراتور های subscript اون رو باز نویسی کنیم. تابع at یه تابعی هست که برای پیاده سازی subscript استفاده شده. این تابع بر خلاف اپراتور subscript اگر خارج از محدوده بخوایم ازش استفاده کنیم exception از نوع Out_of_range میده. برای اینکه این exception رو catch کنید باید از try catch استفاده کنید که توی ویدیوی شماره 6 با هم دیده بودیم.
  8. بعد از وکتور، توی این اسلاید لیست رو بررسی می کنیم. کتابخونه استاندارد لیست دوطرفه هم برای ما فراهم کرده. از لیست برای نگهداری یه sequence از المان هاست. توی لیست insert و delete کردن المانها اون بدون جابجا کردن بقیه المانها توی حافظه امکان پذیره. برای initialize کردن مثل vector از initializer list استفاده می کنیم. اما بر خلاف وکتور برای دسترسی به المانها نمی تونیم از subscript استفاده کنیم و باید کل لیست رو برای پیدا کردن المانی که مورد نظرمون هست search کنیم. برای search کردن باید از اولین المان داخل لیست شروع کنیم و تا زمانی که المان رو پیدا نکردیم یا به انتهای لیست نرسیدیم ادامه میدیم. برای پیمایش مثل همه ی containerها می تونیم از range for استفاده کنیم.
  9. ما میتونیم بجای استفاده کردن از range for که در مثال قبل دیدیم از iterator برای پیمایش لیست استفاده کنیم. می تونیم تابع get_nubmer مثال قبل رو به این صورت بازنویسی کنیم. با استفاده از begin یه interator به ابتدای لیست می گیریم و با استفاده از end چک می کنیم که به انتهای لیست رسیدیم یا نه اگر به انتها نرسیدیم؛ مقدار iterator رو یکی اضافه میکنیم. با ++ کردن یه iterator به المان بعدی در اون container اشاره خواهد کرد. نحوه ی پیاده سازی iterator به صورتی هست که با اون مثل یه pointer باید رفتار کرد. یعنی با اپراتور فلش میشه به اعضای اون المان دسترسی داشت و یا به dereference کردن iterator به خود اون المان دسترسی داشت.
  10. با استفاده از Iterator ها میشه کار های بیشتری انجام داد. در اینجا با استفاده از متد insert میتونیم یه عنصر رو به لیست اضافه کنیم. با داشتن یه iterator میتونیم یه entry رو قبل از اون iterator در لیست قرار بدیم. و با داشتن Iterator می تونیم اون المان رو با استفاده از متد erase از لیست حذف کنیم. اما تفاوت لیست و وکتور چیه. تفاوت اونا توی پیاده سازی اوناست. وکتور های کوچیک با تعداد المانهای کم بهتر از لیست عمل می کنن ولی وقتی تعداد المان ها زیاد میشه و تعداد insert و delete ها زیاد میشه، لیست خیلی بهتر از وکتوره. زمانی که شما به یه sequence از المان ها نیاز دارید باید بین وکتور و لیست یکی رو انتخاب کنید. Performance وکتور برای یه سری از عملیات های پیمایشی مثل findو countو عملیاتهای جستجو و مرتب سازی مثل sort و binary_serach بهتره ولی در لیست هزینه درج و حذف المانها کمتره. این نکات رو در استفاده از این 2 تا container باید در نظر داشته باشید.
  11. در انتها یه جمع بندی بکنیم. این ویدیو در مورد 2 تا از پر کاربردترین containerها کتابخونه استاندارد یعنی وکتور و لیست صحبت کردیم و امکانات اونا رو بررسی کردیم. و نکاتی که در مورد استفاده از اونا باید در نظر گرفت رو هم گفتیم.