What is a Vector?
In C++, a vector refers to a variable used to store multiple values of the same type. Think of a vector as an implementation of an array.
However, unlike an array, a vector is dynamic and can grow or shrink in size as per our requirements.
C++ Create Vector
In C++, vectors are defined in the vector header file included as part of the C++ Standard Library.
To use vectors in C++, we need to include the header file as shown below:
#include <vector>
Once we include the header file, we can declare a vector using the syntax shown below:
std::vector<T> vector_name
The T
parameter in the syntax above refers to any primitive C++ data type.
For example, to declare an int vector, we can do:
#include <iostream>
#include <vector>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
}
In the example above, we declare a vector called my_vector
of type int
. It holds the elements defined inside the curly braces.
C++ Accessing Vector Elements
In C++, we can access the elements of a vector using the at()
method. The function allows us to specify an index and retrieve the value at that index.
Consider the example below:
#include <iostream>
#include <vector>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
std::cout << my_vector.at(1) << std::endl;
}
In the example code above, we use the at()
function to access the element at index 1 of the my_vector
vector.
If we compile and run the code above, we should see an output as shown:
$ g++ vect.cpp
$ ./a.out
2
C++ Print Vector – For Loop
We can iterate over each vector's index using the at()
function and a for loop and return the corresponding element.
Take the illustration shown below:
#include <iostream>
#include <vector>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
for (int i = 0; i < my_vector.size(); i++) {
std::cout << my_vector.at(i) << std::endl;
}
}
In the example above, we use the size()
function to get the size of the vector. This prevents us from manually guessing the size of the vector and accessing out-of-bounds
elements.
We then use each index of the vector in the at()
function to get the corresponding element.
The code above should return:
$ g++ vect.cpp
$ ./a.out
1
2
3
4
5
Instead of the at()
function, you can use vector indexing as shown:
#include <iostream>
#include <vector>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
for (int i = 0; i < my_vector.size(); i++) {
std::cout << my_vector[i] << std::endl;
}
}
The example above uses the indexing notation []
to access the element at the specified index.
C++ Print Vector – For_each
Another standard method of printing a vector is to use an iterator provided in the for_each
method. This is provided in the algorithm library.
An example code is shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
for_each(my_vector.begin(), my_vector.end(), [](const int& n) {
std::cout << n << std::endl;
});
}
The for_each method allows us to apply a function to each element in the vector for a specific range. In this case, from the beginning to the end of the vector.
C++ Print Vector - std::copy()
We can also use the std::copy()
method from the algorithm library to copy the vector elements to stdout.
An illustration is shown below:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main() {
std::vector<int> my_vector = {1,2,3,4,5};
copy(my_vector.begin(), my_vector.end(),
std::ostream_iterator<int(std::cout, " "));
}
C++ Print Vector - Overload std::ostream::operator<<
We can also use the insertion operator to print a vector. This works by overloading the insertion operator to recognize an ostream object and a vector.
Take the example illustration below:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
std::ostream& operator<< (std::ostream& out, const std::vector<int>& vec) {
for (size_t i = 0; i < vec.size(); i++) {
out << vec[i];
if (i < vec.size() - 1) {
out << " ";
}
}
return out;
}
int main() {
std::vector<int> my_vector = {1, 2, 3, 4, 5};
std::cout << my_vector << std::endl;
}
If we compile and run the code above, it should return the elements of the array as shown:
$ g++ vect.cpp
$ ./a.out
1 2 3 4 5
Conclusion
In this article, we discussed several methods of printing a vector in C++.