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.
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.
The push_back function adds an element to the end of a vector.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec;6vec.push_back(10);7vec.push_back(20);8vec.push_back(30);910for (const auto& elem : vec) {11std::cout << elem << " ";12}13return 0;14}
10 20 30
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.
1#include <iostream>2#include <vector>3#include <string>45int main() {6std::vector<std::string> vec;7vec.emplace_back("Hello");8vec.emplace_back("World");910for (const auto& elem : vec) {11std::cout << elem << " ";12}13return 0;14}
Hello World
The pop_back function removes the last element from a vector.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec = {10, 20, 30};6vec.pop_back();78for (const auto& elem : vec) {9std::cout << elem << " ";10}11return 0;12}
10 20
The size function returns the number of elements in a vector.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec = {10, 20, 30};6std::cout << "Size: " << vec.size() << std::endl;7return 0;8}
Size: 3
The capacity function returns the current allocated storage for a vector.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec = {10, 20, 30};6std::cout << "Capacity: " << vec.capacity() << std::endl;7return 0;8}
Capacity: 3
The reserve function allocates memory for at least the specified number of elements, which can improve performance by reducing the need for reallocations.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec;6vec.reserve(100);7std::cout << "Capacity after reserve: " << vec.capacity() << std::endl;8return 0;9}
Capacity after reserve: 100
Vectors support iteration using range-based for loops and traditional iterators.
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec = {10, 20, 30};6for (const auto& elem : vec) {7std::cout << elem << " ";8}9return 0;10}
10 20 30
1#include <iostream>2#include <vector>34int main() {5std::vector<int> vec = {10, 20, 30};6for (auto it = vec.begin(); it != vec.end(); ++it) {7std::cout << *it << " ";8}9return 0;10}
10 20 30
Vectors can contain other vectors, allowing for multi-dimensional data structures.
1#include <iostream>2#include <vector>34int main() {5std::vector<std::vector<int>> matrix = {6{1, 2, 3},7{4, 5, 6},8{7, 8, 9}9};1011for (const auto& row : matrix) {12for (const auto& elem : row) {13std::cout << elem << " ";14}15std::cout << std::endl;16}17return 0;18}
1 2 3 4 5 6 7 8 9
Let's create a simple program that uses vectors to manage a list of students and their grades.
1#include <iostream>2#include <vector>3#include <string>45struct Student {6std::string name;7double grade;8};910int main() {11std::vector<Student> students;1213// Adding students14students.push_back({"Alice", 85.5});15students.push_back({"Bob", 92.0});16students.push_back({"Charlie", 78.0});1718// Displaying students and their grades19for (const auto& student : students) {20std::cout << "Name: " << student.name << ", Grade: " << student.grade << std::endl;21}2223return 0;24}
Name: Alice, Grade: 85.5 Name: Bob, Grade: 92 Name: Charlie, Grade: 78
| Feature | Description |
|---|---|
push_back | Adds an element to the end of the vector. |
pop_back | Removes the last element from the vector. |
size | Returns the number of elements in the vector. |
capacity | Returns the current allocated storage for the vector. |
reserve | Allocates memory for at least the specified number of elements. |
emplace_back | Constructs an element in-place at the end of the vector. |
| Iterators | Allow traversal through elements in a vector using traditional iterator syntax. |
| Vector of Vectors | Enables multi-dimensional data structures by storing vectors within vectors. |
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.