- 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:
- 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:
- 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
- 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.