std::vector to C array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    std::vector to C array

    Is this the correct way to convert a std::vector to a C array or is there a function that returns the vector as an array?

    Code:
    std::vector<int> MyVector;
    //Fill MyVector
    
    int a[MyVector.size()];
    for(int i = 0; i < MyVector.size(); i++)
         a[i] = MyVector[i];
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Bookmark this site and no, there is no such function.
    What you did is basically corrrect.

    kind regards,

    Jos

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      Almost, but a static array can't have a variable size. The number of elements in a must be a literal. You should use a dynamic array for this if you don't know how many items are in MyVector:
      Code:
      int *a = new int[MyVector.size()];
      // ...
      delete[] a;
      Hope this helps.

      Comment

      • MrPickle
        New Member
        • Jul 2008
        • 100

        #4
        What's the difference between my way and your way?

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          An array declared with the int arr[size] syntax must have a constant size, as the compiler must know how much stack space to allocate. If you want to declare an array of variable length (say, the user inputs it), you have to use the new operator, as the provided code did. The site you were linked to earlier contains a very good tutorial for it under its Documentation link.

          Comment

          • MrPickle
            New Member
            • Jul 2008
            • 100

            #6
            Okay, thanks.

            Would this be any better (if it's right)
            Code:
            std::vector<int> MyVector;
            //Fill Vector
            
            int *MyArray;
            memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]));
            [EDIT]
            Looking at the site that was linked, is this also correct:
            Code:
            int *MyArray = MyVector.get_allocator().allocate(MyVector.size());

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              Code:
              std::vector<int> MyVector; 
              //Fill Vector 
                
              int *MyArray; 
              memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]));
              This is not right, because you have not allocated space for MyArray to copy MyVector into. You are also not copying the whole vector because you are only copying a block the size of the first element. You would have to do it like this:
              Code:
              std::vector<int> MyVector;
              //Fill Vector.
              int *MyArray = new int[MyVector.size()];
              memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]) * MyVector.size());
              // Other stuff.
              delete[] MyArray;
              Or totally the C way:
              Code:
              std::vector<int> MyVector;
              //Fill Vector.
              int *MyArray = malloc(sizeof(MyVector[0]) * MyVector.size());
              memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]) * MyVector.size());
              // Other stuff.
              free(MyArray);
              But I don't think it's an improvement, because you are using the memcpy function from the C language in your C++ program.
              As for the way with get_allocator, I don't know how it works, but I have a feeling that it won't.
              Why not just stick with
              Code:
              std::vector<int> MyVector;
              //Fill Vector.
              int *MyArray = new int[MyVector.size()];
              for (int i = 0; i < MyVector.size(); i++)
                  MyArray[i] = MyVector[i];
              // Other stuff.
              delete[] MyArray;
              Hope I got all this right and that it's helpful.
              Good luck.

              Comment

              • MrPickle
                New Member
                • Jul 2008
                • 100

                #8
                I wanted to find an alternative way rather than having to loop through and assign each value.

                I think I have found a way
                Code:
                int *MyArray = new int[MyVector.size()];
                std::copy(MyVector.begin(), MyVector.end(), MyArray);

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  One of the things that a vector guarantees is that it uses exactly the same memory layout as an array (one of the things that causes inserting/deleting anywhere but the end of the array to be inefficient).

                  One of the results of this guarantee is that (assuming the vector has 1 or more entries)

                  &MyVector[0]

                  Is a pointer to the array of data in the vector in the same way that given

                  int MyArray[10];

                  MyArray or &MyArray[0]

                  is a pointer to the data in the array.

                  My understanding is that this is guaranteed by the design requirements of the vector template, that is no matter whose implementation you use this should be true if they have written the vector in a compliant manor.

                  What I can not say is if relying on this is considered good practice.

                  Additionally it doesn't work in more than 1 dimension, taht is a vector or vectors does not have the same memory layout as a 2 dimensional array.

                  Comment

                  Working...