Thread: Array from vector.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Array from vector.

    I have a variable called Numbers of type NumberContainer which is defined as
    Code:
    typedef std::vector<unsigned int> NumberContainer;
    and I need to get a pointer to the array. I tried
    Code:
    unsigned int *newNumbers = &Numbers[0]
    but that points only to the first value in Numbers. How can I fix that?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't understand your question
    if you do
    Code:
      ++newNumbers;
    it will point to the second one.
    Kurt

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I really have to wonder why you are using a vector if you want a fixed size array. You can get random access out of a vector too, you know, in constant time.

    If you really wanted to do this, I would allocate an array of the size you need for unsigned ints and then use the copy algorithm to place them (specifically if the array you want starts and ends in the middle of the vector where the other numdbers shouldn't be accessed), but again vectors have everything you need out of arrays and more.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by ZuK
    I don't understand your question
    if you do
    Code:
      ++newNumbers;
    it will point to the second one.
    Kurt
    I don't want access each one, I need to point to the whole array. And no, I can't pass a vector to the function, it's a library that requires an array. If possible I would like to avoid looping through the vector and creating an array from there.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by citizen
    I really have to wonder why you are using a vector if you want a fixed size array. You can get random access out of a vector too, you know, in constant time.
    I use a vector because I like the flexibility it provides, when I create the vector I don't know the size it will take. But then I need to send an array to a function which I don't have the source code of so I thought that there was a way to get a fixed size array from a vector.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > it's a library that requires an array.
    When you pass a fixed size array to a function, it decays into a pointer anyway. so
    unsigned int *newNumbers = &Numbers[0];
    is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM
  5. How can I convert a vector into an array??
    By shanedudddy2 in forum C++ Programming
    Replies: 6
    Last Post: 01-22-2003, 12:15 PM