naming iterators and other datatypes

This is a discussion on naming iterators and other datatypes within the C++ Programming forums, part of the General Programming Boards category; If you have more iterators inside one function, how would you call them? For instance: Code: std::vector<std::string>::iterator vec_iter; std::vector<std::string>::iterator vec2_iter; ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    naming iterators and other datatypes

    If you have more iterators inside one function, how would you call them?

    For instance:

    Code:
    std::vector<std::string>::iterator vec_iter;
    std::vector<std::string>::iterator vec2_iter;
    std::set<std::string>::iterator set_iter;
    Or is there any better system to keep short and 'pretty' names?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    What are the iterators for? That would determine what names are appropriate for them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    For looping through the containers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    For looping through the containers.
    Duh, iterating is probably the most common use of iterators. Perhaps you should read Stroustrup's answer to the FAQ How do you name variables? In particular: "Name a variable (function, type, whatever) based on what it is or does. Choose meaningful name; that is, choose names that will help people understand your program."
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What does the string inside the vector represent? Is it a name? How about cur_name? If you're using the two iterators at the same time, one might be cur_name and the other prev_name. Or perhaps one is source_name and the other target_name. There is usually information about what the iterators hold besides the fact that they are being used as iterators. I assume your vectors and set are named appropriately? If so, you could incorporate those names into the iterators that come from each of them.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is where namespaces can shine, though its not necessarily the best use for them:

    Example:
    Code:
    namespace mylib
    {
      namespace str
      {
        typedef std::vector<std::string> vector;
        typedef std::set<std::string> set;
      }
    }
    Which affords you the ability to make code like this:

    Example:
    Code:
    #include "mylib.h"
    
    using namespace mylib;
    
    int main(void)
    {
      str::set::iterator i;
      str::vector:: iterator p;
    
      return 0;
    }
    Though maybe that is still too cluttered for your liking... I prefer to keep what the iterator belongs to with its class. Except for within the context of the class.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Name the iterators based on the containers they point to.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    Name the iterators based on the containers they point to.
    Isn't that what he has now? Those names tell me nothing about what they're actually used for.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    No, I mean naming them after the specific instance of the container they are used for. For example if you have a container called friends, listing all the buddies in an IM application, you could use an iterator called "friendIterator" to iterate through the container.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21