programming-examples/c++/Others/Access the elements of a vector through an iterator..cpp

46 lines
870 B
C++
Raw Normal View History

2019-11-15 12:59:38 +01:00
Access the elements of a vector through an iterator.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject(10);
vector<int>::iterator p;
int i;
p = vectorObject.begin();
i = 0;
while(p != vectorObject.end()) {
*p = i;
p++;
i++;
}
cout << "Original contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << "\n\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
*p = *p * 2; // change contents of vector
p++;
}
cout << "Modified Contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " "; // display contents of vector
p++;
}
cout << endl;
return 0;
}