#include <vector>#include <iostream>#include <iterator>
int main(int argc, char** argv) {std::vector<int> dest{1,2,3,4,5};std::vector<int> src{6,7,8,9,10};
// Move elements from src to dest.// src is left in undefined but safe-to-destruct state.dest.insert(dest.end(),std::make_move_iterator(src.begin()),std::make_move_iterator(src.end()));
// Print out concatenated vector.std::copy(dest.begin(),dest.end(),std::ostream_iterator<int>(std::cout, "\n"));
return 0;}
#include <vector>#include <iostream>#include <iterator>
int main(int argc, char** argv) {std::vector<std::vector<int>> dest\{\{1,2,3,4,5}, {3,4}};std::vector<std::vector<int>> src\{\{6,7,8,9,10}};
// Move elements from src to dest.// src is left in undefined but safe-to-destruct state.dest.insert(dest.end(),std::make_move_iterator(src.begin()),std::make_move_iterator(src.end()));
return 0;}
vector<int> concat_vector = vector<int>();concat_vector.setcapacity(vector_A.size() + vector_B.size());// Loop for copy elements in two vectors into concat_vector
方法二:通过添加/插入向量B的元素来附加向量A。
// Loop for insert elements of vector_B into vector_A with insert()function: vector_A.insert(vector_A .end(), vector_B.cbegin(), vector_B.cend());
#include <iostream>#include <vector>#include <boost/range/algorithm/copy.hpp>
int main(int, char**) {std::vector<int> a = { 1,2,3 };std::vector<int> b = { 4,5,6 };boost::copy(b, std::back_inserter(a));for (auto& iter : a) {std::cout << iter << " ";}return EXIT_SUCCESS;}
通常人们的意图是组合向量a和b只是迭代它做一些操作。在这种情况下,有荒谬的简单join函数。
#include <iostream>#include <vector>#include <boost/range/join.hpp>#include <boost/range/algorithm/copy.hpp>
int main(int, char**) {std::vector<int> a = { 1,2,3 };std::vector<int> b = { 4,5,6 };std::vector<int> c = { 7,8,9 };// Just creates an iteratorfor (auto& iter : boost::join(a, boost::join(b, c))) {std::cout << iter << " ";}std::cout << "\n";// Can also be used to create a copystd::vector<int> d;boost::copy(boost::join(a, boost::join(b, c)), std::back_inserter(d));for (auto& iter : d) {std::cout << iter << " ";}return EXIT_SUCCESS;}
namespace internal {
// Implementation detail of Concatenate, appends to a pre-reserved vector, copying or moving if// appropriatetemplate<typename Target, typename Head, typename... Tail>void AppendNoReserve(Target* target, Head&& head, Tail&&... tail) {// Currently, require each homogenous inputs. If there is demand, we could probably implement a// version that outputs a vector whose value_type is the common_type of all the containers// passed to it, and call it ConvertingConcatenate.static_assert(std::is_same_v<typename std::decay_t<Target>::value_type,typename std::decay_t<Head>::value_type>,"Concatenate requires each container passed to it to have the same value_type");if constexpr (std::is_lvalue_reference_v<Head>) {std::copy(head.begin(), head.end(), std::back_inserter(*target));} else {std::move(head.begin(), head.end(), std::back_inserter(*target));}if constexpr (sizeof...(Tail) > 0) {AppendNoReserve(target, std::forward<Tail>(tail)...);}}
template<typename Head, typename... Tail>size_t TotalSize(const Head& head, const Tail&... tail) {if constexpr (sizeof...(Tail) > 0) {return head.size() + TotalSize(tail...);} else {return head.size();}}
} // namespace internal
/// Concatenate the provided containers into a single vector. Moves from rvalue references, copies/// otherwise.template<typename Head, typename... Tail>auto Concatenate(Head&& head, Tail&&... tail) {size_t totalSize = internal::TotalSize(head, tail...);std::vector<typename std::decay_t<Head>::value_type> result;result.reserve(totalSize);internal::AppendNoReserve(&result, std::forward<Head>(head), std::forward<Tail>(tail)...);return result;}