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
If you are using C++11, and wish to move the elements rather than merely copying them, you can use std::move_iterator (http://en.cppreference.com/w/cpp/iterator/move_iterator) along with insert (or copy):
This will not be more efficient for the example with ints, since moving them is no more efficient than copying them, but for a data structure with optimized moves, it can avoid copying unnecessary state:
After the move, src's element is left in an undefined but safe-to-destruct state, and its former elements were transfered directly to dest's new element at the end.
If you are interested in strong exception guarantee (when copy constructor can throw an exception):
Similar
append_move
with strong guarantee can't be implemented in general if vector element's move constructor can throw (which is unlikely but still).You can prepare your own template for + operator:
Next thing - just use +:
This example gives output:
I would use the insert function, something like:
You should use vector::insert
Add this one to your header file:
and use it this way:
r will contain [1,2,62]