Hello,
I have an array of float data, say:
const float myArray[5]={1.0f,2.0f,3.0f,4.0f,5.0f};
I now want to "connect" that data to following vector:
const std::vector<float> myVector
I don't want any data to be copied internally. Can I do that?
Thanks!
How to initialize std::vector with array data without internal copy operation
Started by floatingwoods, Nov 16 2012 08:14 AM
7 replies to this topic
Ad:
#3 Members - Reputation: 246
Posted 16 November 2012 - 08:25 AM
Thanks for the quick reply Brother Bob!
I have a collection of functions that expect const vectors as arguments. Instead of rewriting the interfaces for those functions, I thought it would be faster if I can "connect" the buffer to a vector and still use the same functions.
Cheers
I have a collection of functions that expect const vectors as arguments. Instead of rewriting the interfaces for those functions, I thought it would be faster if I can "connect" the buffer to a vector and still use the same functions.
Cheers
#4 Moderators - Reputation: 5027
Posted 16 November 2012 - 08:56 AM
Is there a way to prevent the data from ever being an array, and just to create it in place in the vector? That way you can avoid copying.
If possible, functions should be templates written in terms of iterators rather than directly taking standard containers. This is flexible, because pointers are a type of iterator too.
If possible, functions should be templates written in terms of iterators rather than directly taking standard containers. This is flexible, because pointers are a type of iterator too.
Edited by rip-off, 16 November 2012 - 08:57 AM.
#6 Moderators - Reputation: 1325
Posted 30 November 2012 - 12:20 AM
I think the Kosher way to do this is with a (very) custom allocator that just happens to always return the address of your array and blows-off the free.
Edited by Shannon Barber, 30 November 2012 - 12:21 AM.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
#8 Moderators - Reputation: 1325
Posted 18 December 2012 - 08:39 PM
How about boost::array?
Edited by Shannon Barber, 18 December 2012 - 08:39 PM.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara






