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
grades.push_back(grade);
...

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

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home