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 255 views
1 answer 247 views
1 answer 271 views
1 answer 260 views
260 views asked Apr 28, 2020 by avibootz
1 answer 192 views
192 views asked Mar 14, 2024 by avibootz
1 answer 235 views
1 answer 263 views
263 views asked Jun 14, 2020 by avibootz
...