vectors iterator

This is a discussion on vectors iterator within the C++ Programming forums, part of the General Programming Boards category; ok i'm confused in vectors and iterators my question is, i created two vectors Code: vector<string> inventory; vector<string> words; then ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    vectors iterator

    ok i'm confused in vectors and iterators

    my question is, i created two vectors

    Code:
    vector<string> inventory;
    vector<string> words;
    then created an iterator
    Code:
    vector<string>::iterator invIterator;
    so to which vector does the invIterator belong ? i haven't tried compiling this but will this compile ?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    If you have two vectors of the same type,, I do this:

    Code:
    std::vector<std::string> inven;
    
    std::vector<std::string> names;
    
    std::vector<std::string>::const_iterator invenIter; // works with inven string vector
    std::vector<std::string>::const_iterator namesIter; // works with names string vector
    It is always best to have an iterator to a vector, no matter if they are the same base type like above. If you name you iterators similar to the vector that it represents, you will not get confused when it comes to working with them.
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    ok after coding like yours i can still use invenIter with the names vector right ?
    i mean that im allowed to use it

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by manzoor View Post
    ok after coding like yours i can still use invenIter with the names vector right ?
    i mean that im allowed to use it
    If you wanted to, yes. Obviously it would be confusing and bad.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you called it
    Code:
    vector<string>::iterator vectIterator;
    it would be assumed to be a more general purpose vector iterator.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I usually name my iterators it or iter...

    Quote Originally Posted by manzoor
    so to which vector does the invIterator belong ? i haven't tried compiling this but will this compile ?
    It doesn't belong to anything until you assign an iterator to it.
    i.e.
    Code:
    vector<string>::iterator invIterator = inventory.begin();

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    thanks every one i now understand it :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 12:36 PM

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