Nice iterator naming [closed]

  softwareengineering

How do you name your iterators when you return a begin and an end iterator from a class? Without it sounding clunky, that is.

Example:

typedef std::vector<Idea> Ideas_Type;

Ideas_Type::const_iterator GetIdeasBegin() const;
Ideas_Type::const_iterator GetIdeasEnd() const;

Should it be GetIdeasBeginIter? IdeasBegin?

1

It’s unclear from your question, but if you are thinking of something like this:

class Brain {
  typedef std::vector<Idea> Ideas; // Not "Ideas_Type".  That's why it's capitalized.
  Ideas::const_iterator firstIdea();
  Ideas::const_iterator lastIdea();
}

There’s no need for all that. You may as well say

class Brain {
  ...
  const Ideas &ideas();
}

By returning a constant reference to the collection, it allows callers to process the collection as they see fit without modifying it. If you don’t want to expose the entire vector interface, then you can create a small class like this:

class my_list<T>: private std::vector<T> {
public:
  // whatever methods you like, including begin(), end(), ...
}

And as a matter of style, save “get” methods for legacy languages.

I would mimic the naming done in the STL. Now your classes work with many built-in functions.


  • begin
  • end

  • rbegin
  • rend

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT