How can I convert a vector into an array??

This is a discussion on How can I convert a vector into an array?? within the C++ Programming forums, part of the General Programming Boards category; I need to convert a vector into an array. How would I do this. The vector is of the same ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Question How can I convert a vector into an array??

    I need to convert a vector into an array.
    How would I do this.
    The vector is of the same type but when I try to use it in my function it comes up with error:

    error C2664: 'IntersectedPolygon' : cannot convert parameter 1 from 'class std::vector<struct CVector3,class std::__default_alloc_template<0,0> >' to 'struct CVector3 []'
    So how can I fix that??

    Thanks for any help provided!

    Shanedudddy2!

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Something like this ???

    Code:
    vector<int> avector;
    //Do stuff with avector
    ....
    ....
    int *pArray = new int[avector.size()];
    for (int i = 0; i < avector.size(); i++)
        pArray[i] = avector[i];
    delete [] pArray;
    Well this only works for int:s but with some smaller modifications it should work with user defined datatypes (make sure you overload the copyassigment operator = and the subscipt operator []).

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Thank you

    Thanks I`ll try that looks like it`ll work.
    Hopefully doing that won't slow the program down too much as I`m only calling that when the mouse is clicked.

    If anyone has any ideas to do it with user defined data types please help.
    Thanks.

    Shanedudddy2

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    all you need for rippers program to work with a user defined data type is for assignment (operator =) to work for your type. Your type also needs a default constructor, but this is required to have an array. If you are not going to be calling any operations on the vector that would invalidate iterators while you hold the array you can get away with Type *p = &v[0]; where v is a std::vector. The next version of the standard will explicitly allow this, and to make this break you would need to have a very wierd version of std::vector. Call v.push_back and p could point to some serious nastyness.

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    A vector is basically a standard template library linked list, right? Is there anythign really all that special to it (meaning things that would be extremely difficult for even an advanced programmer to create) or is it just a standard linked list with all the appropriate operators overloaded and what not. I think that would be an excellent project: make a class that mimics stl's vector class. Don't the borland C++ packages come with vector-like classes called TVecorIsArray or something like that?

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>A vector is basically a standard template library linked list, right?
    I doubt it, I can't think of any way to index a linked list in constant time, which a vector is capable of. Linked lists have linear performance when accessing a node. vectors also don't have constant time insertion and deletion from the middle, that's one of the biggies for a linked list, so if vector were implemented with a linked list it would definitely have that feature.
    *Cela*

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    A vector is an STL array like class. The STL class that is a linked list is the list class.

    >>> extremely difficult for even an advanced programmer to create

    Well, it was created by advanced programmers, so the logical answer would be no. If you understand templating, then any of the STL classes would be self writable. The question is why you would want to, the STL routines have been tried and tested by tens of thousands of people and refined over the years.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. convert char array to int array
    By redruby147 in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 12:09 PM
  3. Convert char array to int
    By McFry in forum C Programming
    Replies: 9
    Last Post: 07-12-2007, 01:40 AM
  4. Replies: 2
    Last Post: 03-05-2005, 04:00 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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