• Advertisement
Sign in to follow this  

std::vector vs std::unique_ptr

This topic is 2030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

@rnlf: I googled your question and found this. For those too lazy to click the link, the relevant code is:

#include<iterator>

std::ifstream testFile("testfile", std::ios::binary);
// ...
std::vector<char> fileContents;
fileContents.reserve(fileSize);
fileContents.assign(std::istreambuf_iterator<char>(testFile),
std::istreambuf_iterator<char>());


All credits to wilhelmtell for the code. Whether or not this is more optimal though, is up for debate, as I think it has potential to be slightly slower (but not faster) than just a simple std::unique_ptr (I say it could potentially be slightly slower because it depends on exactly how the C++ implementation implements std::ifstream::read() and the above code... buffering (and copying into a temporary, intermediate buffer) can make a big difference, and I imagine that a single std::ifstream::read() may potentially just read directly into the buffer, while the std::istreambuf_iterator may potentially cause data to be read into an intermediate buffer before copying it over...). If you care that much, just try each one, look at the asm, and profile.

I might just go for a std::unique_ptr, for simplicity's sake. Edited by Cornstalks

Share this post


Link to post
Share on other sites
Advertisement
@Cornstalks: when I initially googled for the topic, I found this stackoverflow thread as well, but just as James McNellis writes a few comments below, this does not actually seem to be standard compliant. I still have to look it up in the standard to get confirmation though.

it seems to me, there really isn't a nice and correct way to achieve this by using a vector (and in fact, there is no real benefit using a vector instead: Freeing of the array is done automatically when it goes out of scope, it is exception safe, it is not easier to make mistakes with the arrays size and indexed access as it would be with a vector and it saves the overhead of the - in this case - unneccessary management data). Plus it has a much more natural syntax that does not feel like abuse of a vector's interface.

So I guess I'll just stick to it. Thank you anyway, I feel much more confident using this versiow now :-)

Cheers
rnlf

Share this post


Link to post
Share on other sites

@Cornstalks: when I initially googled for the topic, I found this stackoverflow thread as well, but just as James McNellis writes a few comments below, this does not actually seem to be standard compliant. I still have to look it up in the standard to get confirmation though.

That was because his first reversion was, which he fixed (see his response to James McNellis's comment).

But yeah, I like std::unique_ptr for this better, anyway. It makes it a little clearer you have no plans on resizing the buffer, anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement