programming-examples/c++/Others/Accessing a Vector Through an Iterator.cpp

44 lines
860 B
C++
Raw Normal View History

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