🔴 Advanced · Lesson 54
Vector in C++ STL
Vector in C++ STL
What is Vector in C++ STL?
Vector in C++ STL
A vector is a dynamic array that can grow or shrink at runtime. It supports fast indexing and easy insertion at the end.
A vector is a dynamic array that can grow or shrink at runtime. It supports fast indexing and easy insertion at the end.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
vector.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| vector | Vector used in Vector in C++ STL programming. |
| push_back | Push_back used in Vector in C++ STL programming. |
| size | Size used in Vector in C++ STL programming. |
| index | Index used in Vector in C++ STL programming. |
| dynamic array | Dynamic array used in Vector in C++ STL programming. |
Syntax / Pattern
vector<int> v; v.push_back(value);
Example Program
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> marks;
marks.push_back(90);
marks.push_back(85);
cout << marks.size();
}
Expected Output
2
Program Explanation
- push_back adds values at the end.
- size() returns number of elements.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- marks list
- dynamic records
- algorithm practice
Common Mistakes
- Accessing invalid index.
- Using vector without #include <vector>.
Practice Tasks
- Store five marks in vector and find total.
- Remove last element using pop_back().
Summary
Vector in C++ STL is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is Vector in C++ STL?
Vector in C++ STL
A vector is a dynamic array that can grow or shrink at runtime. It supports fast indexing and easy insertion at the end.
A vector is a dynamic array that can grow or shrink at runtime. It supports fast indexing and easy insertion at the end.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
vector.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| vector | Vector used in Vector in C++ STL programming. |
| push_back | Push_back used in Vector in C++ STL programming. |
| size | Size used in Vector in C++ STL programming. |
| index | Index used in Vector in C++ STL programming. |
| dynamic array | Dynamic array used in Vector in C++ STL programming. |
Syntax / Pattern
vector<int> v; v.push_back(value);
Example Program
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> marks;
marks.push_back(90);
marks.push_back(85);
cout << marks.size();
}
Expected Output
2
Program Explanation
- push_back adds values at the end.
- size() returns number of elements.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- marks list
- dynamic records
- algorithm practice
Common Mistakes
- Accessing invalid index.
- Using vector without #include <vector>.
Practice Tasks
- Store five marks in vector and find total.
- Remove last element using pop_back().
Summary
Vector in C++ STL is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.