Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to initialize std::vector with array data without internal copy operation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 floatingwoods   Members   -  Reputation: 292

Like
0Likes
Like

Posted 16 November 2012 - 08:14 AM

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!

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8430

Like
3Likes
Like

Posted 16 November 2012 - 08:17 AM

An std::vector assumes ownership of the data and cannot be used to refer to an external buffer. Why do you need the array as an std::vector if you already have it in the array?

#3 floatingwoods   Members   -  Reputation: 292

Like
0Likes
Like

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

#4 rip-off   Moderators   -  Reputation: 8516

Like
3Likes
Like

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.

Edited by rip-off, 16 November 2012 - 08:57 AM.


#5 floatingwoods   Members   -  Reputation: 292

Like
1Likes
Like

Posted 16 November 2012 - 09:02 AM

Thanks Rip-off!!

You are right, and I try to follow that way of doing nowadays. But my old code is a mess... ;)

#6 Shannon Barber   Moderators   -  Reputation: 1383

Like
0Likes
Like

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

#7 Zipster   Crossbones+   -  Reputation: 763

Like
0Likes
Like

Posted 01 December 2012 - 02:49 AM

Vector semantics require that it copy the objects you insert, regardless of how the allocator is implemented. And if you never insert anything, it would always appear/behave empty and not be of much use to anyone.

#8 Shannon Barber   Moderators   -  Reputation: 1383

Like
0Likes
Like

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS