SlideShare a Scribd company logo
1 of 16
Download to read offline
Level up your coding skills with
the C++ Standard Template
Library (STL):
Vector
BY JOYJIT CHOUDHURY
Vector
 Vectors are sequence containers representing arrays that
can change in size
 It allows random access of it’s individual elements just like
an array, but unlike an array it can dynamically grow in
size depending on the requirements. The memory
management is handled automatically
 Defined in the header <vector>
 Belongs to the namespace std
(remember : using namespace std)
Constructing a vector
first
100 100 100 100
second
Insertion at the back
30
myVector.push_back(30);
30
myVector.push_back(25);
25 30
myVector.push_back(80);
25 80
myVector<int>
 push_back(), adds a new element at the end of the vector, after
its current last element.
Deletion from the back
30 25 80
30 25
myVector.pop_back();
30
myVector.pop_back();
 pop_back(), removes the last element of the vector, reducing it’s
size by one.
myVector<int>
Size
 size(), returns the number of elements in a vector.
30 30 25 80
myVector.size() = 0 myVector.size() = 1 myVector.size() = 3
myVector<int> myVector<int> myVector<int>
Resize
 resize(), resizes the vector so that it contains n elements.
 If n is smaller than the current vector size, the content is reduced to
its first n elements, removing those beyond.
10 20 30 40 50 60 myVector.size() = 6
10 20 30 40 myVector.size() = 4
myVector.resize(4);
myVector<int>
Resize
 If n is greater than the current vector size, the content is expanded
by inserting at the end as mush elements as needed to attain the
size n. The new elements as initialised with copies of the value val
(passed as an argument to resize(n,val)), otherwise they are
value-initialised.
10 20 30 0 0 0
myVector.size() = 3
10 20 30
myVector.size() = 6
myVector.resize(6);
myVector<int>
Resize
-1 -1 -1 -1 -1 -1
myVector.size() = 0
myVector.size() = 6
myVector.resize(6,-1);
myVector<int>
Random access of elements
 The operator[] allows random access of the element at position n of the
vector (0 indexed). It returns a reference to the requested element.
 If the requested index n lies after the last element of the vector,
i.e. n > size-1, it causes undefined behaviour. NEVER DO THAT.
myVector<int>
[0] [1] [2] [3] [4] [5] [6] [7] [8]
myVector.size()=9
0 ≤ 𝑛 < (𝑠𝑖𝑧𝑒 = 9)
cout<<myVector[0]; //10 cout<<myVector[5]; //60
Random access of elements
myVector<int>
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
2D Vector
 2D Vectors can be used to represent 2D arrays. Just like a 2D array is
an array of arrays, similarly a 2D Vector is a vector of vectors.
vector<int> V1 vector<float> V2
int
int
int
int
int
int
float
float
float
float
float
float
int int int int
int int int int
int int int int
int int int int
int int int int
vector<vector<int> > V3
2D Vector
0 0 0 0
0
00
00
00
0
n
m
2D Vector
1 1 1 1
1111
1111
matrix[0]
matrix[1]
matrix[2]
[0] [1] [2] [3]
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
 That’s not all. There are many other functions
and techniques that could come in handy.
Read about them on cplusplus.com or
cppreference.com or somewhere else. Just
google it !
Vector

More Related Content

What's hot (20)

Sets in python
Sets in pythonSets in python
Sets in python
 
Linked list
Linked listLinked list
Linked list
 
List in Python
List in PythonList in Python
List in Python
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Fragment
Fragment Fragment
Fragment
 
Data and time
Data and timeData and time
Data and time
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
07 java collection
07 java collection07 java collection
07 java collection
 
Recursion in C++
Recursion in C++Recursion in C++
Recursion in C++
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
python Function
python Function python Function
python Function
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Php array
Php arrayPhp array
Php array
 

Viewers also liked

An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryJoyjit Choudhury
 
Informationstypen in Woerterbuechern - Deutsch
Informationstypen in Woerterbuechern - DeutschInformationstypen in Woerterbuechern - Deutsch
Informationstypen in Woerterbuechern - DeutschJelena Kostic-Tomovic
 
Silverglades Hill Home Kasauli Brochure - Zricks.com
Silverglades Hill Home Kasauli Brochure - Zricks.comSilverglades Hill Home Kasauli Brochure - Zricks.com
Silverglades Hill Home Kasauli Brochure - Zricks.comZricks.com
 
Most reputable companies in the world 2017 global rep trak
Most reputable companies in the world 2017 global rep trakMost reputable companies in the world 2017 global rep trak
Most reputable companies in the world 2017 global rep trakAnt Garcia
 
5 nota faraid syi20503 ashab al furud nifs - thumun
5 nota faraid syi20503 ashab al furud nifs - thumun5 nota faraid syi20503 ashab al furud nifs - thumun
5 nota faraid syi20503 ashab al furud nifs - thumunabuhannan78
 
Satyapriya rajguru: Every day, in one way or another.
Satyapriya  rajguru:  Every day, in one way or another.Satyapriya  rajguru:  Every day, in one way or another.
Satyapriya rajguru: Every day, in one way or another.Satyapriya Rajguru
 

Viewers also liked (18)

Sets
SetsSets
Sets
 
List
ListList
List
 
An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard Library
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Maps
MapsMaps
Maps
 
SPOJ Problem: OLOLO
SPOJ Problem: OLOLOSPOJ Problem: OLOLO
SPOJ Problem: OLOLO
 
Algorithms: II
Algorithms: IIAlgorithms: II
Algorithms: II
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Algorithms: I
Algorithms: IAlgorithms: I
Algorithms: I
 
Informationstypen in Woerterbuechern - Deutsch
Informationstypen in Woerterbuechern - DeutschInformationstypen in Woerterbuechern - Deutsch
Informationstypen in Woerterbuechern - Deutsch
 
Silverglades Hill Home Kasauli Brochure - Zricks.com
Silverglades Hill Home Kasauli Brochure - Zricks.comSilverglades Hill Home Kasauli Brochure - Zricks.com
Silverglades Hill Home Kasauli Brochure - Zricks.com
 
Kenya PBO structure
Kenya PBO structureKenya PBO structure
Kenya PBO structure
 
Most reputable companies in the world 2017 global rep trak
Most reputable companies in the world 2017 global rep trakMost reputable companies in the world 2017 global rep trak
Most reputable companies in the world 2017 global rep trak
 
5 nota faraid syi20503 ashab al furud nifs - thumun
5 nota faraid syi20503 ashab al furud nifs - thumun5 nota faraid syi20503 ashab al furud nifs - thumun
5 nota faraid syi20503 ashab al furud nifs - thumun
 
Business model generation modelo canvas
Business model generation modelo canvasBusiness model generation modelo canvas
Business model generation modelo canvas
 
Khoi cong, Cong ty to chuc su kien khoi cong chuyen nghiep tai hcm
Khoi cong, Cong ty to chuc su kien khoi cong chuyen nghiep tai hcmKhoi cong, Cong ty to chuc su kien khoi cong chuyen nghiep tai hcm
Khoi cong, Cong ty to chuc su kien khoi cong chuyen nghiep tai hcm
 
Cong ty to chuc su kien khoi cong chuyen nghiep tai hcm
Cong ty to chuc su kien khoi cong chuyen nghiep tai hcmCong ty to chuc su kien khoi cong chuyen nghiep tai hcm
Cong ty to chuc su kien khoi cong chuyen nghiep tai hcm
 
Satyapriya rajguru: Every day, in one way or another.
Satyapriya  rajguru:  Every day, in one way or another.Satyapriya  rajguru:  Every day, in one way or another.
Satyapriya rajguru: Every day, in one way or another.
 

Similar to Vector

Vectors data frames
Vectors data framesVectors data frames
Vectors data framesFAO
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data framesExternalEvents
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTVasil Remeniuk
 
Expand/Collapse animation on Android
Expand/Collapse animation on AndroidExpand/Collapse animation on Android
Expand/Collapse animation on AndroidPavlo Dudka
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabkrishna_093
 
Killing The Unit test talk
Killing The Unit test talkKilling The Unit test talk
Killing The Unit test talkdor sever
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
Web Design & Development - Session 6
Web Design & Development - Session 6Web Design & Development - Session 6
Web Design & Development - Session 6Shahrzad Peyman
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in MatlabJorge Jasso
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problemsMake Mannan
 
Matrix algebra in_r
Matrix algebra in_rMatrix algebra in_r
Matrix algebra in_rRazzaqe
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Randa Elanwar
 
ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine Aleksandar Prokopec
 

Similar to Vector (20)

Vector3
Vector3Vector3
Vector3
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
sparse-matrix.ppt
sparse-matrix.pptsparse-matrix.ppt
sparse-matrix.ppt
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWT
 
Expand/Collapse animation on Android
Expand/Collapse animation on AndroidExpand/Collapse animation on Android
Expand/Collapse animation on Android
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Killing The Unit test talk
Killing The Unit test talkKilling The Unit test talk
Killing The Unit test talk
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Web Design & Development - Session 6
Web Design & Development - Session 6Web Design & Development - Session 6
Web Design & Development - Session 6
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
Arrays
ArraysArrays
Arrays
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problems
 
Matrix algebra in_r
Matrix algebra in_rMatrix algebra in_r
Matrix algebra in_r
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine
 

Recently uploaded

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 

Recently uploaded (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Vector

  • 1. Level up your coding skills with the C++ Standard Template Library (STL): Vector BY JOYJIT CHOUDHURY
  • 2. Vector  Vectors are sequence containers representing arrays that can change in size  It allows random access of it’s individual elements just like an array, but unlike an array it can dynamically grow in size depending on the requirements. The memory management is handled automatically  Defined in the header <vector>  Belongs to the namespace std (remember : using namespace std)
  • 3. Constructing a vector first 100 100 100 100 second
  • 4. Insertion at the back 30 myVector.push_back(30); 30 myVector.push_back(25); 25 30 myVector.push_back(80); 25 80 myVector<int>  push_back(), adds a new element at the end of the vector, after its current last element.
  • 5. Deletion from the back 30 25 80 30 25 myVector.pop_back(); 30 myVector.pop_back();  pop_back(), removes the last element of the vector, reducing it’s size by one. myVector<int>
  • 6. Size  size(), returns the number of elements in a vector. 30 30 25 80 myVector.size() = 0 myVector.size() = 1 myVector.size() = 3 myVector<int> myVector<int> myVector<int>
  • 7. Resize  resize(), resizes the vector so that it contains n elements.  If n is smaller than the current vector size, the content is reduced to its first n elements, removing those beyond. 10 20 30 40 50 60 myVector.size() = 6 10 20 30 40 myVector.size() = 4 myVector.resize(4); myVector<int>
  • 8. Resize  If n is greater than the current vector size, the content is expanded by inserting at the end as mush elements as needed to attain the size n. The new elements as initialised with copies of the value val (passed as an argument to resize(n,val)), otherwise they are value-initialised. 10 20 30 0 0 0 myVector.size() = 3 10 20 30 myVector.size() = 6 myVector.resize(6); myVector<int>
  • 9. Resize -1 -1 -1 -1 -1 -1 myVector.size() = 0 myVector.size() = 6 myVector.resize(6,-1); myVector<int>
  • 10. Random access of elements  The operator[] allows random access of the element at position n of the vector (0 indexed). It returns a reference to the requested element.  If the requested index n lies after the last element of the vector, i.e. n > size-1, it causes undefined behaviour. NEVER DO THAT. myVector<int> [0] [1] [2] [3] [4] [5] [6] [7] [8] myVector.size()=9 0 ≤ 𝑛 < (𝑠𝑖𝑧𝑒 = 9) cout<<myVector[0]; //10 cout<<myVector[5]; //60
  • 11. Random access of elements myVector<int> [0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
  • 12. 2D Vector  2D Vectors can be used to represent 2D arrays. Just like a 2D array is an array of arrays, similarly a 2D Vector is a vector of vectors. vector<int> V1 vector<float> V2 int int int int int int float float float float float float int int int int int int int int int int int int int int int int int int int int vector<vector<int> > V3
  • 13. 2D Vector 0 0 0 0 0 00 00 00 0 n m
  • 14. 2D Vector 1 1 1 1 1111 1111 matrix[0] matrix[1] matrix[2] [0] [1] [2] [3] [0][0] [0][1] [0][2] [0][3] [1][0] [1][1] [1][2] [1][3] [2][0] [2][1] [2][2] [2][3]
  • 15.  That’s not all. There are many other functions and techniques that could come in handy. Read about them on cplusplus.com or cppreference.com or somewhere else. Just google it !