Vectors
In C++, a vector is a dynamic list of items that can shrink and grow in size. It can only store values of the same data type. Memory management is handled internally, and elements can be added or removed efficiently.
Vectors are widely used in real-world C++ applications, including game development (for managing game objects), financial modeling (to store variable-length time series), data processing (to handle datasets of unknown size), and competitive programming (due to their ease of use and dynamic nature).
Creating a Vector
Vectors are defined using the <vector>
library and are part of the std
namespace:
#include <vector>std::vector<type> name;
When a vector is created, the data type of its elements must be specified. The type cannot be changed afterward.
Creating a Vector with Size
#include <vector>std::vector<int> grades(10);
Creating and Initializing a Vector
#include <vector>std::vector<double> order = {3.99, 12.99, 2.49};
Accessing and Modifying Elements in a Vector
Elements in a vector can be accessed and modified using:
Using the Index []
Operator
#include <vector>#include <iostream>int main() {std::vector<double> order = {3.99, 12.99, 2.49};order[0] = 4.23;std::cout << order[0];return 0;}
Output:
4.23
Using the .at()
Function
#include <vector>#include <iostream>int main() {std::vector<double> order = {3.99, 12.99, 2.49};order.at(0) = 4.23;std::cout << order[0];return 0;}
Output:
4.23
Alternatives to .at()
include .size()
, .capacity()
, .front()
, and .back()
.
Adding Elements to a Vector
Elements can be inserted into a vector using:
.push_back()
method.emplace_back()
method
Using the .push_back()
Method
#include <vector>#include <iostream>int main() {std::vector<int> v = {1,2,3,4};v.push_back(5);std::cout << "The last element is: " << v[4];return 0;}
Output:
The last element is: 5
Using the .emplace_back()
Method
#include <vector>#include <iostream>int main() {std::vector<int> v = {1,2,3,4};v.emplace_back(5);std::cout << "The last element is: " << v[4];return 0;}
Output:
The last element is: 5
The .insert()
method is another alternative for inserting elements.
Removing Elements from a Vector
The .pop_back()
method removes the last element:
#include <vector>#include <iostream>int main() {std::vector<int> v = {1,2,3,4};v.pop_back();for (int i : v) {std::cout << i << " ";}return 0;}
Output:
1 2 3
Iterating Over a Vector
The foreach
loop can be used to iterate over a vector:
#include <vector>#include <iostream>int main() {std::vector<int> v = {1,2,3,4};for (int i : v) {std::cout << i << " ";}return 0;}
Output:
1 2 3 4
Codebyte Example: Common Vector Operations
Frequently Asked Questions
1. What does vector .size()
return in C++?
The .size()
function returns the number of items stored in the vector:
#include <vector>#include <iostream>int main() {std::vector<int> v = {1,2,3,4};std::cout << "Size: " << v.size();return 0;}
Output:
Size: 4
2. Are C++ vectors ordered?
Yes. Elements are stored and accessed in the order they were inserted.
3. Are vectors thread-safe?
No. Vectors are not thread-safe by default. Synchronization mechanisms must be used for concurrent access.
Vectors
- .assign()
- Assigns new values to the elements of a vector, replacing its current contents.
- .back()
- Used to access the last element in a vector.
- .capacity()
- Returns the currently allocated storage space for a vector.
- .clear()
- Removes all of the elements in the vector.
- .emplace()
- Constructs and inserts an element in place at a specified position.
- .empty()
- Returns a boolean indicating whether the vector is empty or not.
- .erase()
- Removes a single element or a range of elements from a vector.
- .front()
- Returns a reference to the first element in the vector.
- .size()
- Returns the number of elements in the vector.
All contributors
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
- Christine_Yang
- christian.dinh
- Sriparno08
- Arinze_Obi
- garanews
- hazeltonbw
- Anonymous contributor
- Anonymous contributor
- THE-Spellchecker
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours