🔴 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.
Level
🔴 STL, Modern C++ and Projects
Example File
vector.cpp
Main Focus
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

TermMeaning / Use
vectorVector used in Vector in C++ STL programming.
push_backPush_back used in Vector in C++ STL programming.
sizeSize used in Vector in C++ STL programming.
indexIndex used in Vector in C++ STL programming.
dynamic arrayDynamic 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

  1. Store five marks in vector and find total.
  2. 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.
Level
🔴 STL, Modern C++ and Projects
Example File
vector.cpp
Main Focus
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

TermMeaning / Use
vectorVector used in Vector in C++ STL programming.
push_backPush_back used in Vector in C++ STL programming.
sizeSize used in Vector in C++ STL programming.
indexIndex used in Vector in C++ STL programming.
dynamic arrayDynamic 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

  1. Store five marks in vector and find total.
  2. 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.

← Back to C++ Tutorial