Two Questions In One

This is a discussion on Two Questions In One within the C++ Programming forums, part of the General Programming Boards category; Hello all. It's been a while since I last posted here but I'm back in the swing of things and ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    Thumbs up Two Questions In One [RESOLVED]

    Hello all. It's been a while since I last posted here but I'm back in the swing of things and I have two doozies for any of you if you have the time.

    First off I have a question about the vector STL. I know that single dimensional vectors have the iterators that can navigate both forward and backward though the vector and you can also access the vector elements with subscripts. But I was wondering about multidimensional vectors. My question about these is two fold. Can you use an iterator to navigate through a multidimensional vector or do you have to use the two for loops to flip though the vector in a row-by-row fashion? And if you can use an iterator, can you get away with using only one or do you have to use two iterators, one for each dimension?

    Second is a question about nested classes. I was playing around with these in a very small program that I figured would allow me to test out the concepts and see for myself. I wrote the program and it compiled fine however the output was in no way what I was expecting. Here is the program:

    Code:
    #include <cstdlib>
    #include <iostream> 
    using namespace std; 
    
    class A {
      protected:
             int d;     
      public:
             class SubA {
                 protected:
                     int a;
                 public:
                     SubA() {};
                     ~SubA() {};
                     int getA() { return a; }
                     void setA(int _a) { _a = a; }
             };
             class SubB {
                 protected:
                     int b;
                 public:
                     SubB() {};
                     ~SubB() {};
                     int getB() { return b; }
                     void setB(int _b) { _b = b; }
             };
             class SubC {
                 protected:
                     int c;
                 public:
                     SubC() {};
                     ~SubC() {};
                     int getC() { return c; }
                     void setC(int _c) { _c = c; }
             };
                      A() {};
                     ~A() {};
             int getD() { return d; }
             void setD(int _d) { _d = d; }
     };
      int main(int argc, char *argv[]) {
         //instantiate all the classes
         A dummy;
         A::SubA subADummy;
         A::SubB subBDummy;
         A::SubC subCDummy;
    
        //set up some values
         dummy.setD(5);
         subADummy.setA(10);
         subBDummy.setB(15);
         subCDummy.setC(20);
    
         //print them out to the screen
         cout << dummy.getD() << endl;
         cout << subADummy.getA() << endl;
         cout << subBDummy.getB() << endl;
         cout << subCDummy.getC() << endl;
    
         system("PAUSE");
         return EXIT_SUCCESS;
     }
    Now as I'm sure you can see from the code the output that I was expecting. However what I got was totally different. It looked more like this:

    2013315389
    2013327107
    2013454920
    7863840

    Because the numbers are related somehow (the first three anyway) I'm guessing that this is a location in memory rather than the actual values I passed to the classes. Or I could be wrong. I'm not too sure. But if I could get some help with these topics I'd appreciate it. Thanks in advance. It's much appreciated.
    Last edited by xmltorrent; 01-09-2008 at 12:53 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Can you use an iterator to navigate through a multidimensional vector or do you have to use the two for loops to flip though the vector in a row-by-row fashion?

    You have to go through each dimension separately.

    >> And if you can use an iterator, can you get away with using only one or do you have to use two iterators, one for each dimension?

    You have to have two different iterators, since they will have different types (one will be vector<T>::iterator and one will be vector<vector<T> >::iterator).

    Code:
    void setA(int _a) { _a = a; }
    Your assignment is backwards, you should be assigning to a, not to _a. Fix that for all the nested classes and it should work better. As it is now, the member variables are uninitialized, so your output is whatever junk is in memory at those locations. I would suggest adding them to initializer lists in your constructors so that they will be initialized even if the user of the class doesn't call the set function.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    haha That nested class problem was goofy. I didn't realize I was doing that. I was just typing away and didn't notice until you looked at it. Sometimes it's truly better to have a fresh set of eyes looking at something.

    Thanks for all of your help.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you use an iterator to navigate through a multidimensional vector
    No, because there isn't a multidimensional vector. It's a vector of vectors [of vectors...], and you have to treat it as such.

    >And if you can use an iterator, can you get away with using only
    >one or do you have to use two iterators, one for each dimension?
    You need two iterators:
    Code:
    std::vector<std::vector<int> > v;
    
    //...
    
    std::vector<std::vector<int> >::iterator row = v.begin();
    
    while ( row != v.end() ) {
      std::vector<int>::iterator col = row->begin();
    
      while ( col != row->end() )
        std::cout<< *col++ <<'\n';
    
      ++row;
    }
    >I'm guessing that this is a location in memory rather
    >than the actual values I passed to the classes.
    Look closely at your setters. You're assigning the data member to the parameter, not the other way around.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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