By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,724 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,724 IT Pros & Developers. It's quick & easy.

std::vector to C array

MrPickle
100+
P: 100
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?

Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector;
  2. //Fill MyVector
  3.  
  4. int a[MyVector.size()];
  5. for(int i = 0; i < MyVector.size(); i++)
  6.      a[i] = MyVector[i];
  7.  
Oct 27 '08 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Bookmark this site and no, there is no such function.
What you did is basically corrrect.

kind regards,

Jos
Oct 27 '08 #2

boxfish
Expert 100+
P: 469
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:
Expand|Select|Wrap|Line Numbers
  1. int *a = new int[MyVector.size()];
  2. // ...
  3. delete[] a;
  4.  
Hope this helps.
Oct 27 '08 #3

MrPickle
100+
P: 100
What's the difference between my way and your way?
Oct 27 '08 #4

Expert 100+
P: 849
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.
Oct 27 '08 #5

MrPickle
100+
P: 100
Okay, thanks.

Would this be any better (if it's right)
Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector;
  2. //Fill Vector
  3.  
  4. int *MyArray;
  5. memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]));
[EDIT]
Looking at the site that was linked, is this also correct:
Expand|Select|Wrap|Line Numbers
  1. int *MyArray = MyVector.get_allocator().allocate(MyVector.size());
Oct 27 '08 #6

boxfish
Expert 100+
P: 469
Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector; 
  2. //Fill Vector 
  3.  
  4. int *MyArray; 
  5. 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:
Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector;
  2. //Fill Vector.
  3. int *MyArray = new int[MyVector.size()];
  4. memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]) * MyVector.size());
  5. // Other stuff.
  6. delete[] MyArray;
Or totally the C way:
Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector;
  2. //Fill Vector.
  3. int *MyArray = malloc(sizeof(MyVector[0]) * MyVector.size());
  4. memcpy(MyArray, &(MyVector[0]), sizeof(MyVector[0]) * MyVector.size());
  5. // Other stuff.
  6. 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
Expand|Select|Wrap|Line Numbers
  1. std::vector<int> MyVector;
  2. //Fill Vector.
  3. int *MyArray = new int[MyVector.size()];
  4. for (int i = 0; i < MyVector.size(); i++)
  5.     MyArray[i] = MyVector[i];
  6. // Other stuff.
  7. delete[] MyArray;
Hope I got all this right and that it's helpful.
Good luck.
Oct 28 '08 #7

MrPickle
100+
P: 100
I wanted to find an alternative way rather than having to loop through and assign each value.

I think I have found a way
Expand|Select|Wrap|Line Numbers
  1. int *MyArray = new int[MyVector.size()];
  2. std::copy(MyVector.begin(), MyVector.end(), MyArray);
Oct 28 '08 #8

Banfa
Expert Mod 5K+
P: 8,965
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.
Oct 28 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.