SlideShare a Scribd company logo
1 of 12
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, algorithmsVahid Heidari
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.pptTekle12
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
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 manual2Mouna Guru
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
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

17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlockVahid Heidari
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streamsVahid 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 (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

Incident handling is a clearly defined set of procedures to manage and respon...
Incident handling is a clearly defined set of procedures to manage and respon...Incident handling is a clearly defined set of procedures to manage and respon...
Incident handling is a clearly defined set of procedures to manage and respon...Varun Mithran
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 
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
 
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
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
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
 
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
 
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
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14VMware Tanzu
 
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
 
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
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 

Recently uploaded (20)

Incident handling is a clearly defined set of procedures to manage and respon...
Incident handling is a clearly defined set of procedures to manage and respon...Incident handling is a clearly defined set of procedures to manage and respon...
Incident handling is a clearly defined set of procedures to manage and respon...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
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
 
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
 
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
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
 
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
 
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
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
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...
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
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 ...
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 

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ها کتابخونه استاندارد یعنی وکتور و لیست صحبت کردیم و امکانات اونا رو بررسی کردیم. و نکاتی که در مورد استفاده از اونا باید در نظر گرفت رو هم گفتیم.