codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

64 / 87 topics
62Introduction to STL & Containers63std::array64Vectors65List & Forward List66Deque67Stack68Queue & Priority Queue69Map & Multimap70Set & Multiset71Unordered Map & Unordered Multimap72Unordered Set & Unordered Multiset73Iterators74Algorithms75Functors
Tutorials/C++ Programming/Vectors
⚡C++ Programming

Vectors

Updated 2026-05-12
30 min read

Vectors

In this tutorial, we will explore the std::vector container in C++, which is part of the Standard Template Library (STL). Vectors are dynamic arrays that can grow and shrink in size as needed, making them a versatile choice for handling collections of data. Understanding vectors is crucial for any serious C++ programmer.

Introduction

Vectors provide a way to store elements in a contiguous block of memory, similar to an array. However, unlike fixed-size arrays, vectors can dynamically resize themselves when more elements are added or removed. This makes them ideal for scenarios where the number of elements is not known beforehand or may change during program execution.

In this tutorial, we will cover various operations and features of std::vector, including adding and removing elements, accessing elements, checking size and capacity, and iterating through vectors. We'll also explore more advanced topics like nested vectors and efficient memory management using the reserve function.

Core Content

Adding Elements

push_back

The push_back function adds an element to the end of a vector.

vector_push_back.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec;
6 vec.push_back(10);
7 vec.push_back(20);
8 vec.push_back(30);
9
10 for (const auto& elem : vec) {
11 std::cout << elem << " ";
12 }
13 return 0;
14}
Output
10 20 30

emplace_back

The emplace_back function constructs an element in-place at the end of the vector, which can be more efficient than push_back for complex types.

vector_emplace_back.cpp
1#include <iostream>
2#include <vector>
3#include <string>
4
5int main() {
6 std::vector<std::string> vec;
7 vec.emplace_back("Hello");
8 vec.emplace_back("World");
9
10 for (const auto& elem : vec) {
11 std::cout << elem << " ";
12 }
13 return 0;
14}
Output
Hello World

Removing Elements

pop_back

The pop_back function removes the last element from a vector.

vector_pop_back.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 vec.pop_back();
7
8 for (const auto& elem : vec) {
9 std::cout << elem << " ";
10 }
11 return 0;
12}
Output
10 20

Size and Capacity

size

The size function returns the number of elements in a vector.

vector_size.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 std::cout << "Size: " << vec.size() << std::endl;
7 return 0;
8}
Output
Size: 3

capacity

The capacity function returns the current allocated storage for a vector.

vector_capacity.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 std::cout << "Capacity: " << vec.capacity() << std::endl;
7 return 0;
8}
Output
Capacity: 3

reserve

The reserve function allocates memory for at least the specified number of elements, which can improve performance by reducing the need for reallocations.

vector_reserve.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec;
6 vec.reserve(100);
7 std::cout << "Capacity after reserve: " << vec.capacity() << std::endl;
8 return 0;
9}
Output
Capacity after reserve: 100

Iterating Through Vectors

Vectors support iteration using range-based for loops and traditional iterators.

Range-Based For Loop

vector_range_based_for.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 for (const auto& elem : vec) {
7 std::cout << elem << " ";
8 }
9 return 0;
10}
Output
10 20 30

Traditional Iterators

vector_iterators.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 for (auto it = vec.begin(); it != vec.end(); ++it) {
7 std::cout << *it << " ";
8 }
9 return 0;
10}
Output
10 20 30

Vector of Vectors

Vectors can contain other vectors, allowing for multi-dimensional data structures.

vector_of_vectors.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<std::vector<int>> matrix = {
6 {1, 2, 3},
7 {4, 5, 6},
8 {7, 8, 9}
9 };
10
11 for (const auto& row : matrix) {
12 for (const auto& elem : row) {
13 std::cout << elem << " ";
14 }
15 std::cout << std::endl;
16 }
17 return 0;
18}
Output
1 2 3 
4 5 6 
7 8 9

Practical Example

Let's create a simple program that uses vectors to manage a list of students and their grades.

student_grades.cpp
1#include <iostream>
2#include <vector>
3#include <string>
4
5struct Student {
6 std::string name;
7 double grade;
8};
9
10int main() {
11 std::vector<Student> students;
12
13 // Adding students
14 students.push_back({"Alice", 85.5});
15 students.push_back({"Bob", 92.0});
16 students.push_back({"Charlie", 78.0});
17
18 // Displaying students and their grades
19 for (const auto& student : students) {
20 std::cout << "Name: " << student.name << ", Grade: " << student.grade << std::endl;
21 }
22
23 return 0;
24}
Output
Name: Alice, Grade: 85.5
Name: Bob, Grade: 92
Name: Charlie, Grade: 78

Summary

FeatureDescription
push_backAdds an element to the end of the vector.
pop_backRemoves the last element from the vector.
sizeReturns the number of elements in the vector.
capacityReturns the current allocated storage for the vector.
reserveAllocates memory for at least the specified number of elements.
emplace_backConstructs an element in-place at the end of the vector.
IteratorsAllow traversal through elements in a vector using traditional iterator syntax.
Vector of VectorsEnables multi-dimensional data structures by storing vectors within vectors.

What's Next?

Now that you have a solid understanding of std::vector, it's time to explore other containers like std::list and std::forward_list. These containers offer different performance characteristics and are suitable for various use cases. Continue your journey in the next tutorial on List & Forward List.


Previousstd::arrayNext List & Forward List

Recommended Gear

std::arrayList & Forward List