How do I concatenate two std::vector
s?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Or you could use:
This pattern is useful if the two vectors don't contain exactly the same type of thing, because you can use something instead of std::back_inserter to convert from one type to the other.
Here's a general purpose solution using C++11 move semantics:
Note how this differs from
append
ing to avector
.With range v3, you may have a lazy concatenation:
Demo.
A general performance boost for concatenate is to check the size of the vectors. And merge/insert the smaller one with the larger one.
With C++11, I'd prefer following to append vector b to a:
when
a
andb
are not overlapped, andb
is not going to be used anymore.This is
std::move
from<algorithm>
, not the usualstd::move
from<utility>
.