Concatenating two std::vectors

后端 未结 25 2758
予麋鹿
予麋鹿 2020-11-22 12:00

How do I concatenate two std::vectors?

25条回答
  •  面向向阳花
    2020-11-22 12:22

    I've implemented this function which concatenates any number of containers, moving from rvalue-references and copying otherwise

    namespace internal {
    
    // Implementation detail of Concatenate, appends to a pre-reserved vector, copying or moving if
    // appropriate
    template
    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::value_type,
                        typename std::decay_t::value_type>,
                "Concatenate requires each container passed to it to have the same value_type");
        if constexpr (std::is_lvalue_reference_v) {
            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)...);
        }
    }
    
    template
    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
    auto Concatenate(Head&& head, Tail&&... tail) {
        size_t totalSize = internal::TotalSize(head, tail...);
        std::vector::value_type> result;
        result.reserve(totalSize);
        internal::AppendNoReserve(&result, std::forward(head), std::forward(tail)...);
        return result;
    }
    

提交回复
热议问题