我如何连接两个std::向量?
当前回答
c++ 17中有一个算法std::merge,当输入向量排序时,它非常容易使用,
下面是例子:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
//DATA
std::vector<int> v1{2,4,6,8};
std::vector<int> v2{12,14,16,18};
//MERGE
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
//PRINT
for(auto item:dst)
std::cout<<item<<" ";
return 0;
}
其他回答
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
使用c++ 20,你可以去掉带范围的begin()和end()。
#include <ranges>
std::ranges::copy(vec2, std::back_inserter(vec1));
或者如果你想移动元素:
std::ranges::move(vec2, std::back_inserter(vec1));
我已经实现了这个函数,它连接任何数量的容器,从右值引用移动和复制
namespace internal {
// Implementation detail of Concatenate, appends to a pre-reserved vector, copying or moving if
// appropriate
template<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;
}
我将使用插入函数,类似于:
vector<int> a, b;
//fill with data
b.insert(b.end(), a.begin(), a.end());
如果你正在寻找的是在创建后将一个向量附加到另一个向量的方法,vector::insert是你最好的选择,因为已经回答了几次,例如:
vector<int> first = {13};
const vector<int> second = {42};
first.insert(first.end(), second.cbegin(), second.cend());
遗憾的是,没有办法构造一个const vector<int>,就像上面那样,你必须先构造然后插入。
如果你实际上是在寻找一个容器来保存这两个vector<int>s的连接,可能有更好的可用的东西给你,如果:
你的向量包含原语 包含的原语的大小为32位或更小 你需要一个const容器
如果以上都是正确的,我建议使用basic_string,它的char_type匹配vector中包含的原语的大小。你应该在你的代码中包含一个static_assert来验证这些大小保持一致:
static_assert(sizeof(char32_t) == sizeof(int));
有了这一点,你可以这样做:
const u32string concatenation = u32string(first.cbegin(), first.cend()) + u32string(second.cbegin(), second.cend());
要了解更多关于string和vector之间区别的信息,您可以查看这里:https://stackoverflow.com/a/35558008/2642059
有关此代码的实际示例,您可以在这里查看:http://ideone.com/7Iww3I