← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 2 of 15

C++ vector

A vector is a list that grows and shrinks as you add or remove items, like a stretchy shopping list. You can reach any item by its position.
Syntax
cpp
#include <vector>

std::vector<data_type> vector_name = {value1, value2};
vector_name.push_back(value);
vector_name.pop_back();
vector_name[index];

What is a vector?

A vector is C++'s dynamic array template class: unlike a fixed-size raw array, a vector automatically grows or shrinks as elements are added or removed, managing its own underlying heap memory without you having to call new or delete yourself.

Example: What is a vector?

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums;
	nums.push_back(1);
	nums.push_back(2);
	// Print to the console with cout
	std::cout << nums.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Adding and Removing Elements

You add an element to the end of a vector with push_back() and remove the last element with pop_back(); both operations automatically adjust the vector's reported size, so you never manually track how many elements it currently holds.

Example: Adding and Removing Elements

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2};
	nums.push_back(3);
	nums.pop_back();
	// Print to the console with cout
	std::cout << nums.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Elements

You can access an element with plain index brackets like vec[i], or with the at() method, which is safer because it performs bounds checking and throws an out_of_range exception rather than silently reading invalid memory for a bad index.

Example: Accessing Elements

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {10, 20, 30};
	// Print to the console with cout
	std::cout << nums[1] << " " << nums.at(2) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Vector Capacity vs Size

Size is the number of elements a vector actually currently holds, while capacity is the total memory it has reserved, which is often larger.

Vectors deliberately over-allocate capacity in advance so that most push_back() calls don't need to trigger an expensive reallocation.

Example: Vector Capacity vs Size

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3};
	// Print to the console with cout
	std::cout << "size=" << nums.size() << " capacity=" << nums.capacity() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Iterating over Vectors

You can iterate over a vector's elements with a traditional index-based loop, a range-based for loop for cleaner syntax, or explicit STL iterators — all three work interchangeably and the choice mostly comes down to readability and whether you need the index itself.

Example: Iterating over Vectors

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < nums.size(); i++) std::cout << nums[i] << " ";
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using v[i] past the end, which is undefined behavior; v.at(i) throws instead.
  2. Keeping an iterator or reference after push_back causes the vector to reallocate, which invalidates it.
  3. Confusing size() (elements stored) with capacity() (space reserved).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.