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 160 views
1 answer 159 views
1 answer 175 views
1 answer 165 views
165 views asked Apr 28, 2020 by avibootz
1 answer 114 views
114 views asked Mar 14, 2024 by avibootz
1 answer 139 views
1 answer 172 views
172 views asked Jun 14, 2020 by avibootz
...