Actually a Java programmer I am dealing with C++ again. Haven't done so in 15 years, so please be a little easy on me when I start off with the probably most asked question about C++ possible:
That is the neverending story of dynamically figuring out the size of primary type arrays. (I know, I know...sorry)
I was well-behaved and searched the forum first. I see that things haven't changed and it's still not possible to dynamically figure out the size of an primary type array as can be seen here: http://www.cplusplus.com/forum/beginner/18760/
Unfortunately I am about to make use of an lib that needs this type of array. That is: TYPE[n] with n being unkown at running time. Concret:
1 2 3
double xDoubles[] = <list of n double values gathered somewhere else>
...
xValues.setcontent(0, numberOfValues, xDoubles);
I'd make use of std::vector<T> http://www.cppreference.com/wiki/stl/vector/start
but I can't find a way (method) to convert from std::vector<T> back to double[]. Hm, no this shouldn't work anyway.
So how to solve this problem?
Greatly appreciate any hints (links, etc.)!
You guys have prompted me with the same solution, I guess. And actually the one I had in mind and feared to be the only one. That is to iterate through std::vector and build up a double[]. Okay then, I go for this one.
Many thanks!!
PS: Hoped that C++ would keep track of these things (length) so costs of figuring this out would be 1. Now it is n. But okay, I am dealing with array sizes around 50 < n < 200. So it'll be hardly costly.
I don't understand your fear because I don't understand the original question. The vector does keep track of its own length. You want to make a double from a vector? Why? If you are going to build a new object and copy then of course you have to copy each element n times. What we have said is that the address of the first element is a pointer to type T (double, int, Foo, or whatever T is). Just to be clear, in our examples you are passing a pointer to the first element of the array to the func. you are not copying the vector unless the func does a copy internally. If we are misunderstanding your requirement please clarify your question with a better example.
PS: Hoped that C++ would keep track of these things (length) so costs of figuring this out would be 1. Now it is n. But okay, I am dealing with array sizes around 50 < n < 200. So it'll be hardly costly.
The std::vector does keep track of its length. So the cost of figuring it out is 1, not n. ;o)
I was missing the fact that Galik's code isn't actually building up a double[]. Instead vector<double> is treated like double[]. This is great. Because limiting any effort to zero. Sorry about my then confusing last post. Didn't know vector<double> IS or at least can be treated like a double[].
I was missing the fact that Galik's code isn't actually building up a double[]. Instead vector<double> is treated like double[]. This is great. Because limiting any effort to zero. Sorry about my then confusing last post. Didn't know vector<double> IS or at least can be treated like a double[].
The reason this is possible is because the C++ standard guarantees that the storage in a std::vector is contiguous (all in one block). This means that a std::vector must be implemented as an array internally. However, beware that taking the address of that array using &v[0] will not be valid if you modify your std::vector because it may re-allocate its internal array giving it a new address.
So this is a useful technique for interfacing with legacy code that still uses arrays but is otherwise best avoided.