How to print vector using for loop in C++

2 Answers

0 votes
#include <iostream>  
#include <vector> 

using std::vector;
using std::cout;
using std::endl;

int main()
{
	vector<int> v{ 0, 1, 2, 3, 4 };

	for (int i = 0; i < v.size(); i++)
		cout << v[i] << endl;

	return 0;
}

/*
run:

0
1
2
3
4

*/

 



answered Apr 24, 2018 by avibootz
0 votes
#include <iostream>  
#include <vector> 

using std::vector;
using std::cout;
using std::endl;

int main()
{
	vector<int> v{ 0, 1, 2, 3, 4 };

	for (int n : v)
		cout << n << endl;

	return 0;
}

/*
run:

0
1
2
3
4

*/

 



answered Apr 24, 2018 by avibootz

Related questions

1 answer 173 views
1 answer 178 views
1 answer 191 views
1 answer 178 views
178 views asked Apr 28, 2020 by avibootz
1 answer 121 views
121 views asked Mar 14, 2024 by avibootz
1 answer 152 views
1 answer 184 views
184 views asked Jun 14, 2020 by avibootz
...