Four Ways to Loop Over A Vector

The article will give the code about four ways to loop over a Vector. In the meantime, I also calculate the time for looping over a Vector in order to get the comparison between difference way.

Below is the code to loop over a vector.

#include <iostream>
#include <vector>
#include <ctime>
#include <chrono>
using namespace std;

void createData(vector<int>&amp; vec, int number)
{
	vec.clear();
	for (int i = 0; i < number; i++) {
		vec.push_back(i);
	}
}

auto startRun()
{
	auto start = std::chrono::high_resolution_clock::now();
	return start;
}
void stopRun(auto&amp; start)
{
	auto end = std::chrono::high_resolution_clock::now();
	std::chrono::duration<double, std::milli> float_ms = end - start;
	cout << "Time usage is " << float_ms.count() << " milliseconds" << endl;
} 
int main()
{
	vector<int> test;

	createData(test, 1000);

	auto start = startRun();
	//The first way to loop over the vector. index.
	for (int i = 0; i < test.size(); i++) {
		cout << test[i] << " ";
	}
	cout << endl;
	stopRun(start);

	//The second way to loop over the vector. iterator.
	start = startRun();
	for (vector<int>::iterator iter	= test.begin(); iter != test.end(); iter++) {
		cout << (*iter) << " ";
	}
	cout << endl;
	stopRun(start);

	//The third way to loop over the vector. Auto with iterator.
	start = startRun();
	for (auto iter = test.begin(); iter != test.end(); iter++) {
		cout << (*iter) << " ";
	}
	cout << endl;
	stopRun(start);

	//The forth way to loop over the vector. Auto.
	start = startRun();
	for(auto iter : test) {
		cout << iter << " ";
	}
	cout << endl;
	stopRun(start);
}

Output:

From the result, we can know there is no big difference in time consuming between difference ways.

You can copy the code and run it in your Linux system. Also, you can change the numbers of vector and run multiple times to see the result.

You will find the time usage is not the same when you run mulitple times with difference vector size.

When I tried to run the code 3 times and the vector size is 50. Below is the result.

First:

Time usage is 0.040683 milliseconds
Time usage is 0.00866 milliseconds
Time usage is 0.008412 milliseconds
Time usage is 0.00749 milliseconds

Second:
Time usage is 0.708365 milliseconds
Time usage is 0.062051 milliseconds
Time usage is 0.060025 milliseconds
Time usage is 0.05891 milliseconds

Third:
Time usage is 0.040883 milliseconds
Time usage is 0.008722 milliseconds
Time usage is 0.00852 milliseconds
Time usage is 0.007685 milliseconds

When I tried to run the code 3 times and the vector size is 5000. Below is the result.

First:

Time usage is 2.28577 milliseconds
Time usage is 18.1697 milliseconds
Time usage is 4.97572 milliseconds
Time usage is 1.74201 milliseconds

Second:

Time usage is 0.921591 milliseconds
Time usage is 1.40455 milliseconds
Time usage is 1.51804 milliseconds
Time usage is 1.43088 milliseconds

Third:

Time usage is 4.05851 milliseconds
Time usage is 1.73607 milliseconds
Time usage is 1.52674 milliseconds
Time usage is 1.65401 milliseconds

So, we don’t need to care the time usage, we can use the most convenient way to loop over a Vector. I’d like to use auto method. How about you??

Note:

You should add c++14 flag when you compile the above code. Please refer to below command:

g++ loopovervector.cpp -std=c++14

In the above code, if you would like to use auto as a keyword, you should use -std=c++11. If you would like to use auto as return type and parameter type, you should use -std=c++14.

Share this article to your social media
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments