Recommendation for moving / shifting vector members?
Linda A. Walsh
gcc@tlinx.org
Thu Jul 17 22:37:00 GMT 2014
Oleg Endo wrote:
> The generic version is probably std::rotate
> http://en.cppreference.com/w/cpp/algorithm/rotate
---
Ahhh... with random shuffling and partition in the C++
book... hmmm... not the first place I would have thought.
I would have thought rotate woudl be treated like shift.
(i.e. both supported or not).
It's not real clear why rotate is provided but not shift,
other than the "organizational hint" being that rotate can't
(or doesn't) use movmem like primitives, but by touching
each element via the algorithm along the lines of random
shuffling or partitioning(?).
Isn't it just a matter of moving something off one end vs.
to the other end of the list?
>
>> I'm not sure if memmove on the members is supported or not?
>> Are they supposed to be contiguous in memory and memmove is
>> 'fine' -- and I'm overthinking this, or is there better
>> solution?
>
> The elements stored in a vector are in contiguous memory. That's one
> key property of the container. See also the description in
> http://en.cppreference.com/w/cpp/container/vector
---
I thought I remember reading that, but wasn't sure if locations
(beginning of 'extra capacity' or such) in the vector were stored
somewhere, such that my moving the memory directly might not corrupt
the container.
Good to know. In my use case I'd hoped to use
memmove if shift wasn't available. Though the is_trivial
has me a bit worried now that you mention it, since
a single data point is actually a
struct{ Timespec x; valarray<uint64_t> D;}.
Not sure if the valarray is considered trivial ..
am *guessing* yes, but will have to try it out.
> If the std::vector class itself is a custom made specialization, things
> could be a bit difficult to handle in a generic way from outside the std
> lib implementation.
---
Understandable if an object requires non trivial initialization,
but even a valarray of 'ints' requires a length and could be
viewed as non-trivial in that case. Is there a better way of
knowing if something is trivial than writing a test program?
Will have to explore the cppref site more, as it seems you
recommend it...
Thanks!
More information about the Libstdc++
mailing list