How to access the elements of a vector through an iterator in C++

1 Answer

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

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

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5 };
	vector<int>::iterator it;

	it = vec.begin();
	while (it != vec.end()) {
		*it = *it * 2;           
		it++;
	}

	it = vec.begin();
	while (it != vec.end()) {
		cout << *it << " ";    
		it++;
	}

	cout << endl;

	return 0;
}


/*
run:

2 4 6 8 10

*/

 



answered May 19, 2018 by avibootz

Related questions

1 answer 207 views
1 answer 272 views
2 answers 204 views
2 answers 228 views
1 answer 129 views
1 answer 188 views
1 answer 166 views
...