iterators into vector of vector??

This is a discussion on iterators into vector of vector?? within the C++ Programming forums, part of the General Programming Boards category; if I have a vector and I wanted an iterator, I would do something like the following: Code: vector<int> myVector; ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    iterators into vector of vector??

    if I have a vector and I wanted an iterator, I would do something like the following:

    Code:
    vector<int> myVector;
    vector<int>::iterator it = myVector.begin();
    But if I have a vector of vectors like follows:

    Code:
    vector<vector<int> > myVector2;
    How can I get an iterator for this?
    Thanks
    Mark

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Have you tried this?
    vector < vector < int > > :: iterator it = myVector.begin();

    here's one quick and sweet example:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main ( ) 
    {
        int i, j;
        vector < vector < int > > matrix;
        vector < vector < int > > :: iterator it;
        vector <int> :: iterator it2;
        matrix.resize(5);
        for (i = 0; i < 5; i++)
            matrix[i].resize(5);
             
        for (i = 0; i < 5; i++)
            for (j = 0; j < 5; j++)
                matrix[i][j] = i*j;
        
        for (it = matrix.begin(); it != matrix.end(); ++it, cout<<endl)
            for (it2 = it->begin();it2 != it ->end(); ++it2)
                cout <<*it2<<" ";
        return 0;
    }
    Last edited by Micko; 06-26-2006 at 05:21 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    The point being that an iterator is a compound type. In order for an object of this type to be defined, it is necessary to include the iterator keyword and the type of the iterator itself. Hence being a compound type.

    The type of an iterator, is always the type of the container to which the iterator points:

    vector<int>
    An iterator to this container is of the type vector<int>

    vector< vector<int> >
    An iterator to this cointainer is of the type vector < vector<int> >. You can traverse the "mother" container.
    An iterator to the underlying cointainer is of type vector<int>. You can traverse the "child" container.

    Mother
    and child here being loosely used
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Perhaps std::map would be more suited to the task. A vector of vectors is quite confusing.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Probably is, but maybe he's just curious...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Maybe he just wants a matrix
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Mario F.
    The point being that an iterator is a compound type. In order for an object of this type to be defined, it is necessary to include the iterator keyword and the type of the iterator itself. Hence being a compound type.
    Iterator is not a keyword, just a convention. And compound type means something else.

    The type of an iterator, is always the type of the container to which the iterator points:
    The type of an iterator is the type of the iterator. It's value type is the same as that of the container it is meant for.

    vector<int>
    An iterator to this container is of the type vector<int>
    No, it's of the type vector<int>::iterator. This type is opaque: it could be something as simple as int*, it could also be an instantiation of the __gnu_cxx::__normal_iterator template.
    The iterator's value type is int.

    vector< vector<int> >
    An iterator to this cointainer is of the type vector < vector<int> >. You can traverse the "mother" container.
    An iterator to the underlying cointainer is of type vector<int>. You can traverse the "child" container.
    Same thing. The iterator's value type in this case is vector<int>, i.e. dereferencing the iterator yields a vector, which you could in turn traverse.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Quote Originally Posted by CornedBee
    Iterator is not a keyword, just a convention. And compound type means something else.
    A compound type is loosely meant as a type defined in terms of another type (Lipmann and others) . A far-reach compound type is a class. An iterator is a compound type.

    However, I do agree it's strecthing the meaning.

    Quote Originally Posted by CornedBee
    The type of an iterator is the type of the iterator. It's value type is the same as that of the container it is meant for.
    I meant the abstract notion of the iterator. Not the iterator object itself.

    The rest of my post follows on this notion.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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