Marilag Ang Malaya

Sunday, February 8, 2009

From C pointers to C++ iterators

Yesterday, we discussed C++ vectors as one of the ways to improve the representation of an C array. C++ iterators allow easy access to elements in a vector safely. However, one must not mistake an iterator to be a pointer, even if it acts like one. Additionally, your vanilla pointers still exist in C++. Don't forget to put #include <vector>
std::vector<int> grades;   // dynamically-sized vector

// push items into the vector

for (std::vector<int>::iterator grade_itr=grades.begin();
grade_itr < grades.end();
std::cout << (*grade_ptr);

Labels: , , ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home