Thread: vector of pointers

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    vector of pointers

    how do I get the value of 'a' from the vector ?

    Code:
    int a = 5, *b = &a;
    vector<int*> v;
    v.push_back(b);
    cout << ?
    edit:
    found how:
    *v[0]
    Last edited by jaymz; 10-12-2011 at 05:24 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Why not store integers in the vector instead of pointers to integers?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Quote Originally Posted by VirtualAce View Post
    Why not store integers in the vector instead of pointers to integers?
    I agree with you. I would totally avoid using pointers in this case, but unfortunately I am forced to

    Now I discovered another inconvenience of doing this. I am now pushing more values into it. While iterating through this vector, it only prints out the last value pushed for each v[i] vector position. How to fix this ? Maybe I'm not pushing correctly and my vector has the same (last pushed) value all over ?

  4. #4
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by jaymz View Post
    I agree with you. I would totally avoid using pointers in this case, but unfortunately I am forced to

    Now I discovered another inconvenience of doing this. I am now pushing more values into it. While iterating through this vector, it only prints out the last value pushed for each v[i] vector position. How to fix this ? Maybe I'm not pushing correctly and my vector has the same (last pushed) value all over ?
    Let me guess - you're pushing a pointer to the same value each and every time? Perhaps you should post your code so that we can see what's going on...
    Last edited by gardhr; 10-12-2011 at 06:36 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Putting the address of a local variable inside a container of pointers, is a huge red flag.
    Show us some code and we'll work out how you should be doing it.
    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. vector containing pointers to another vector
    By yes2 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2010, 03:55 PM
  2. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 03:29 PM
  3. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  4. vector of pointers vs vector of values
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 02:27 PM
  5. Vector of pointers
    By Hunter2 in forum Windows Programming
    Replies: 5
    Last Post: 08-19-2002, 09:42 AM