0

I have a very long double array (>10000) that has read data from a datafile. I want to convert it into std::vector to be able to use some relevant functions.

Is there any better and efficient way to do it than copying each data one by one?

Thanks in advance.

  • 6
    Why don't you read into the `vector` in first place. – Gaurav Sehgal Apr 13 '18 at 09:51
  • 2
    *"very long double array (>10000)"* - Unless you have hundreds of millions of double, copying a double array to a vector should be pretty insignificant. – Holt Apr 13 '18 at 09:52
  • what function you need? I don't think there would be much. – apple apple Apr 13 '18 at 09:53
  • `myVector.resize(arrayLen); memcpy(&myVector[0], array, arrayLen * sizeof(double));` – Nadir Apr 13 '18 at 09:54
  • Indeed, as long as it's not in a hot loop, copying some 10k doubles does not matter performance wise. – Baum mit Augen Apr 13 '18 at 09:54
  • 3
    @Nadir Answer belong into the answer section. Not least because you can downvote answers, unlike comments, which would be warranted given that completely superfluous use of `memcpy`. Also, your solution needlessly writes the memory twice. – Baum mit Augen Apr 13 '18 at 09:57
  • @BaummitAugen how do you copy all contents from an array into a vector without allocating memory first for that? – Nadir Apr 13 '18 at 10:02
  • 2
    This. I dont get why every second answer on this side includes memcpy. I have never used this function every in my life. – informant09 Apr 13 '18 at 10:02
  • 2
    @informant09 There are some (very rare) valid usecases. But this is not one of them. – Baum mit Augen Apr 13 '18 at 10:03
  • @Nadir Read the dupe target. – Baum mit Augen Apr 13 '18 at 10:04
  • @BaummitAugen I have gone throught the most voted answer, which uses the constructor with 2 iterators (begin and end). Browsing the source code of vector (for windows), they iterate from begining to end emplacing back, allocating memory when needed, so doing the same in the very end. I dont see any performance gain from that – Nadir Apr 13 '18 at 10:10

1 Answers1

1

You have two ways to handle this:

  1. Work on pairs of iterators instead of the vector object you'd have to create. This allows you to move to a container later without any code changes, but requires you to pass the begin and end of the range as raw pointers into the array.

  2. Read into a container in the first place and not bother with an array at all.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • You can also read into an array, and later build the `vector` with a custom allocator that "adopts" the array - see https://stackoverflow.com/questions/21917529/is-it-possible-to-initialize-stdvector-over-already-allocated-memory . – fwyzard Apr 13 '18 at 10:02
  • @fwyzard, you're right, you could use an `array_view`. Abusing an allocator for that seems... contrived and unexpected. How would e.g. `resize` function if there is still another pointer variable to the underlying array? – rubenvb Apr 13 '18 at 10:20