Iterate across a C++ Vector via ‘for’ Loop
A for loop is a looping construct in C++ which allows you to repeat a block of code as many times as you specify. A for loop is often used to iterate through a vector, since it can easily be repeated a specific number of times.
To iterate along a C++ vector via for loop, you need to define the vector and its elements. Vectors are defined using the <vector> library, and they can store any type of data or object, from primitive data types to complex objects. You then need to specify how many elements the vector contains, and what data type each element is.
There are several methods to iterate through a C++ vector in a for loop, which are as follows:
1: Iterate across a C++ Vector Using for Loop and Indexing
We loop over the vector using indexing the most frequently. With the C++ programming language, we need to know the length of the vector to iterate across it.
#include <vector>
using namespace std;
int main()
{
vector<int> vect = {23,53,2,3,25,54};
for (int x = 0; x < vect.size(); x++) {
cout << vect[x] << " ";
}
return 0;
}
In the above code, we first construct an integer vector called vect and give it some starting values. The vector is then iterated through using a for loop. The loop executes each element in the vector since it runs from x=0 to the vect.size(). We access each element inside the loop using the subscript operator [] and print it to the console using cout.
Output
2: Iterate Across a C++ Vector Using for Loop and Reference Pointer
When iterating over a vector using a for loop and reference pointer, a loop is used to access each element of the vector via its position and each element’s value is accessed directly through the reference pointer. Use caution while using reference pointers to avoid mistakenly modifying the wrong memory locations.
#include <vector>
using namespace std;
int main()
{
vector<int> vect = {11,12,13,14,15};
int& ref = vect[0];
for (int i = 0; i < vect.size(); i++) {
cout << ref << " ";
ref++;
}
return 0;
}
In the above code, we first build and initialize a vector of numbers called vect. Finally, using vect[0], we declare a reference pointer ref and initialize it with the vector’s first member. Each vector element is accessed inside the for loop using the reference pointer ref, and it is then printed to the console using cout. The ref++ operator is also used to advance the reference pointer to the vector’s subsequent element.
Output
3: Iterate Across a C++ Vector Using for Loop and Iterators
The vector class in C++ gives us two methods that we may use to obtain the vector’s start and end iterators, which are begin() and end(). The pointer referring to the start of the vector is obtained using the begin() method, and the pointer pointing to the end of the vector is obtained using the end() function. With this, we can loop through the vector and use the output function to display the value. We’ll make sense of this with an example.
#include <vector>
using namespace std;
int main()
{
vector<int> v = {43, 23, 64, 45, 72};
vector<int>::iterator vect;
for(vect = v.begin(); vect != v.end(); vect++)
{
cout<<*vect<<" ";
}
return 0;
}
An iterator is utilized as a pointer to loop across a vector in this program. The pointer is subsequently increased to access the next vector element. The character * is used to access the memory location that the iterator is pointing at its value.
Output
4: Iterate across a C++ Vector Using for Loop Using auto Keyword
A vector can also be traversed using the auto keyword. The accessible element and the sequence that must be iterated must be stored in a variable provided to auto. It has to be utilized along with a for loop, as seen below.
#include <vector>
using namespace std;
int main()
{
vector<int> v = {43, 3, 23, 54};
for (auto& a : v)
{
cout << a << " ";
}
return 0;
}
The auto keyword was used in the code above to iterate through the vector items. Therefore, the type of elements in the vector will be chosen automatically. Each element is taken and made available for the activities we wish to perform on it.
Output
Conclusion
Iterating through a C++ vector via a for loop is a simple and efficient way to access and manipulate the elements of a vector. By properly defining the parameters in the for loop, you can ensure that your code will execute as expected and terminate when all the iterations have completed. With a bit of practice and understanding of the C++ language, you can master the for loop and use it to iterate through a vector in your own programs.