Thread: STL Vector question

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    11

    STL Vector question

    Hi all,

    Just having a semantics issue with STL. Given a declaration as per the following:

    Code:
    	typedef std::vector <int> fooData;
    	std::vector <fooData> m_fooData;
    I can access the data by using the following code:

    Code:
            m_fooData[0][0];
    However I can't get my head around how I access the data in the following scenario (or if it's even valid):
    Code:
    	typedef std::vector <int> fooData;
    	std::vector <fooData *> m_fooData;
    Can anyone point out what i'm misunderstanding?

    Cheers!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    (*m_foodata[0])[0]
    I think.

    Storing a pointer to a vector seems a bit useless to me - unless you have a scenario where you have a large existing vector (or a large number of existing vectors) that you need to insert into a class - but that seems a bit bass ackwards too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    Yeah - maybe i'm going about this wrong...

    What i'm trying to do is store a vector of vectors of type int. Which is fine, given this scenario:

    Code:
    	typedef std::vector <int> fooData;
    	std::vector <fooData> m_fooData;
    However, I don't know how many "fooData"'s there is going to be until runtime. So in that case how would I populate my vector of ints, then push_back that vector to the vector vectors? Is this valid?

    Code:
          fooData temp;
    
          while (...) 
          {
               x = "some calculation";
    
               temp.push_back(x);
               m_fooData.push_back(temp);
               temp.clear();
          }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ Programming.

    Quote Originally Posted by Beamu
    However, I don't know how many "fooData"'s there is going to be until runtime. So in that case how would I populate my vector of ints, then push_back that vector to the vector vectors? Is this valid?
    Well... the idea is on the right track, but the code certainly is not valid. What is suspect in your idea is that you push to the back of temp on each iteration of the loop, and also push temp to the back of m_fooData on each iteration.

    What you probably want to do is to have nested loops. In the outer loop, you create a new fooData object named temp and then run the inner loop. In the inner loop, you perform the calculation and then push the int result to the back of temp on each iteration. Immediately after control leaves the inner loop you push the fooData object to the back of m_fooData, and only then do you end the current iteration of the outer loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    Yup - thanks, you're right there - that was pretty poor pseudo code. But the concept is sound. Is that because the value of temp is copied during the push_back call?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Beamu
    Yup - thanks, you're right there - that was pretty poor pseudo code.
    Besides the obvious syntax errors and the potential problem with your idea as expressed in the code that I pointed out, note that it would be better to create temp in the loop so as to follow the principle of declaring variables near first use. This would also make the use of the clear() member function unnecessary.

    Quote Originally Posted by Beamu
    But the concept is sound. Is that because the value of temp is copied during the push_back call?
    Yes, copying happens with push_back(), but I am not sure how you relate that to the soundness of the concept. The soundness of the concept is due to to use of push_back() to dynamically insert an element at runtime.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Besides the obvious syntax errors and the potential problem with your idea as expressed in the code that I pointed out, note that it would be better to create temp in the loop so as to follow the principle of declaring variables near first use. This would also make the use of the clear() member function unnecessary.
    Yup agreed. Assume I understood what you said, and that my original pseudo code was just plain terrible.

    Yes, copying happens with push_back(), but I am not sure how you relate that to the soundness of the concept. The soundness of the concept is due to to use of push_back() to dynamically insert an element at runtime.
    Soundness in that my original naive concern was that by continually using the same vector to store data then push that back, I would be changing the original vector and thereby corrupting the data. When I wrote the (admittedly misleading pseudo code above) it was because I'd suddenly realised it does nothing of the sort since it is copied by the push_back method.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It would actually be better to push_back an empty fooData and then fill the one that's in the outer vector. This avoids the costly copy of the entire vector.
    Code:
    for(outer loop) {
      m_fooDatas.push_back(fooData());
      fooData &cur = m_fooDatas.back();
      for(inner loop) {
        cur.push_back(whatever);
      }
    }
    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

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CornedBee View Post
    It would actually be better to push_back an empty fooData and then fill the one that's in the outer vector. This avoids the costly copy of the entire vector.
    Code:
    for(outer loop) {
      m_fooDatas.push_back(fooData());
      fooData &cur = m_fooDatas.back();
      for(inner loop) {
        cur.push_back(whatever);
      }
    }
    Either that, or just swap it in instead:
    Code:
    for(outer loop) {
      fooData cur;
      for(inner loop) {
        cur.push_back(whatever);
      }
      m_fooDatas.push_back(fooData());
      m_fooDatas.back().swap(cur);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM