push_back with pointers on STL vectors

This is a discussion on push_back with pointers on STL vectors within the C++ Programming forums, part of the General Programming Boards category; I'm going crazy here, I'd really appreciate some help. I have the following piece of code: Code: vector<Type*> a_vector; for ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    push_back with pointers on STL vectors

    I'm going crazy here, I'd really appreciate some help. I have the following piece of code:

    Code:
    vector<Type*> a_vector;
    
    for (int i=0; i<n; i++) {
    	Type* ptr = new Type(this);
    	a_vector.push_back(ptr);
    	printf("i: %d, ptr: %p\n", i, ptr);
    }
    
    printf("size: %d, a_vector[0]: %p\n", a_vector.size(), a_vector[0]);

    And I get this result:

    Code:
    i: 0, ptr: 0x80c8530
    i: 1, ptr: 0x80c8648
    i: 2, ptr: 0x80c8738
    i: 3, ptr: 0x80c8828
    
    size: 4, a_vector[0]: (nil)

    I don't get it: I print ptr before doing push_back and it isn't nil, the final size of a_vector is 4, which shows it has indeed inserted all the pointers, but then the first one (and the others too, actually) is nil. How is this possible? Thanks a lot.

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Ok, don't mind me. I'm stupid. *sigh*. In case anyone's wondering, the problem was that I resized a_vector and then added elements at the back. In my defense I must say that it's been a looong loooooong time since I last used the STL (or C++, for that matter), and I had a really big test this morning, so my brain isn't working that good.

    The problem is solved now but I feel really stupid

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    why are you using printf?

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    indigo0086, if you don't know what printf does you should google it. (google.com) I hope that helps!

    Anyway... I think his point is that you should use cout (for c++ projects) and not printf? I'll be specific for you since he wasn't

    "printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()):"

    http://www.parashift.com/c++-faq-lit....html#faq-15.1

    :-)
    Last edited by simpleid; 06-15-2007 at 10:52 AM.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was just saying, using the C++ STL while using a C standard output format seems counter intuitive

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    that does seem counter intuitive, i agree. too bad 90&#37; of the c++ tutorials i search through support it. the craziness. i prefer cout.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Use std::cout instead.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    I know, I know, I'm not actually going to print anything in the final program, I was just checking on the values of my variables. I like cout way better than printf myself but it's been so long since I programmed in C++ and I'm so used to C my mind plays tricks on me sometimes. Thanks for the suggestion anyway.

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    And you can always do something like
    [code]vector.push_back(new type value) or something. Instead of having an intermediate value to push back. Don't forget to traverse the vector and delete any values when not needed, since just deleting a record will not do that for you.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    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. Objects or pointers in STL containers?
    By zacs7 in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2009, 10:25 AM
  2. Sets of Vectors in STL
    By Steff in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2009, 12:03 AM
  3. problems with stl vectors function push_back
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2009, 02:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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