std::copy与std::back_inserter引发的惨案

std,copy,back,inserter,引发,惨案 · 浏览次数 : 4

小编点评

The problem with the code lies in the `copy` function. The issue with the assignment operator (`=`), the `last` parameter being passed to the function and the lack of clear initialization of `d_first` are causing unexpected behavior. The corrected code using `insert` is: ```cpp std::vector v{1, 2, 3, 4, 5}; std::vector copied{begin(v), end(v)}; v.insert(end(v), begin(copied), end(copied)); ``` In this corrected version, the elements are added to the `copied` vector using the `insert` function, which takes the start and end iterators of the source and destination vectors and inserts the elements from the source vector into the destination vector. This ensures that the elements are added to the end of the `copied` vector as intended.

正文

#include <iostream>
#include <vector>
#include <numeric>
#include <sstream>


int main() {
    std::vector v{1, 2, 3, 4, 5};
    std::copy(begin(v), end(v), std::back_inserter(v));

    for (const auto& item : v)
        std::cout << item << ' ';
}

原本目的是将vector中的元素再复制一遍,追加到其末尾,然而却发现新增的元素并不是预期中的(并且每次运行都不一样)。大概看了一下copy的实现才发现问题(copy实际源码挺复杂的,进行了不少优化,仅提供简化版本):

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last,
              OutputIt d_first)
{
    for (; first != last; (void)++first, (void)++d_first)
        *d_first = *first;
 
    return d_first;
}

仔细可以注意到,last是会发生变化的,所以造成了结果不符合预期。

 

可以改用insert实现:

std::vector v{1, 2, 3, 4, 5};
std::vector<int> copied{begin(v), end(v)};
v.insert(end(v), begin(copied), end(copied));

与std::copy与std::back_inserter引发的惨案相似的内容:

std::copy与std::back_inserter引发的惨案

#include #include #include #include int main() { std::vector v{1, 2, 3, 4, 5}; std::copy(begin(v), end(v), std

迭代器的一些简单理解

迭代器的一些简单理解 使用迭代器最方便的地方就是和算法库结合,对于实现只需要聚焦于算法,而不用过多考虑数据结构的实现。 举一个常见的的例子,std::copy_n 用作于范围元素的复制,适配于各个容器类型,并且演化出了 back_inserter/front_inserter/inserter 这类

何时/如何使用 std::enable_shared_from_this

要点回顾 继承自 std::enable_shared_from_this 的类能够在其自身实例中通过 std::shared_from_this 方法创建一个指向自己的 std::shared_ptr 智能指针。 从一个裸指针创建多个 std::shared_ptr 实例会造成严

std::for_each易忽略点

以下代码为修改vector内部的每一个元素,使其每个元素大小变为原来的平方。 std::vector v1{1, 2, 4, 2}; std::for_each(begin(v1), end(v1), [](auto& n) { return n * n; }); for (const auto&

用现代C++写一个python的简易型list

std::variant介绍:en.cppreference.com/w/cpp/utility/variant 通过泛型模板(仅提供了int, double, string三种类型的存储),实现了append, pop, front, back, size等方法,并且通过重载运算符实现了对负数索引

从优秀源码中学到的两个技巧

设计一个不能被using的对象 在实际开发中为了避免命名空间污染,一般都不会using namespace std。但是如果一个对象写起来比较复杂,用using能大幅度地简化操作。现在假设我们要设计一个函数,它在一个作用域里面,使用它只能以A::B::C()这种形式。思考一下,如果我们放在命名空间下

SDL3 入门(3):三角形

SDL3 提供了 SDL_RenderGeometry 函数绘制几何图形,用法和 OpenGL 差不多,先定义顶点数据,然后根据顶点数据绘制几何图形。 绘制三角形的代码如下: std::array origin_vertices = { SDL_Vertex { { 1

C++11智能指针 unique_ptr、shared_ptr、weak_ptr、循环引用、定制删除器

目录智能指针场景引入 - 为什么需要智能指针?内存泄漏什么是内存泄漏内存泄漏的危害内存泄漏分类如何避免内存泄漏智能指针的使用及原理RAII简易例程智能指针的原理智能指针的拷贝问题智能指针的发展历史std::auto_ptr模拟实现auto_ptr例程:这种方案存在的问题:Boost库中的智能指针un

题解:CF1956A Nene's Game

这道题其实挺有意思,多测里面还套了个多测。 思路就是用向量模拟删除过程,具体请看代码里的注释。 #include using namespace std; int k,q,a[105]; void solve() { int n; cin>>n; vector

模板特化的多维度挖掘

假如我有一个需求,就是如果传入的参数是int类型,我就输出int类型,否则就输出T。很显然,根据模板的基础知识,我们可以这么写 template void f(T) { std::cout << "T\n"; } template <> void f(int) { std::co