首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移出某些元素后无法从矢量中删除

移出某些元素后无法从矢量中删除
EN

Stack Overflow用户
提问于 2019-09-22 19:32:29
回答 1查看 63关注 0票数 0

你好,我正在尝试理解如何优化从一个向量到另一个向量的移动元素,检查这是不是在调用复制或移动构造函数。

但是,当我试图擦除移出到另一个向量v2上的std::vector v1中的元素时,编译器会报告以下消息:

代码语言:javascript
复制
vector-move-elements.cpp:19:5: note: copy assignment operator is implicitly deleted because 'Foo' has a
      user-declared move constructor
    Foo(Foo&& other) {
    ^

以下是代码

代码语言:javascript
复制
#include <vector>
#include <string>
#include <iostream>
// #include <algorithm> std::copy_if

// inspired from https://stackoverflow.com/questions/15004517/moving-elements-from-stdvector-to-another-one
struct Foo
{
    Foo(std::string&& s, uint32_t idx) {
        this->name = std::move(s);
        this->index = std::move(idx);
        std::cout << "Calling default constructor " << name << idx << std::endl;
    }
    Foo(const Foo& other) {
        this->name = other.name;
        this->index = other.index;
        std::cout << "Calling copy constructor " << other.name << other.index << std::endl;
    }
    Foo(Foo&& other) {
        this->name = std::move(other.name);
        this->index = std::move(other.index);
        std::cout << "Calling move constructor " << other.name << other.index << std::endl;
    }
    std::string name;
    uint32_t index;
};

int main() {
    std::vector<Foo> v1, v2;
    v1.reserve(25); // https://stackoverflow.com/questions/52109542/push-back-to-stdvector-the-copy-constructor-is-repeatedly-called
    v2.reserve(25); // without this the push_back instruction causes a call to copy constructor

    // fill v1 with dummy elements
    for (uint32_t i = 0; i < 25; ++i ) {
        std::string s = std::string("name_") + std::to_string(i);
        std::cout << s << std::endl;
        v1.emplace_back(Foo(std::move(s), i));
    }

    std::cout << "End of construction" << std::endl;

    // populate v1 with at least 17 elements...
    const auto it = std::next(v1.begin(), 17); // same as v1.begin()+17;
    std::move(v1.begin(), it, std::back_inserter(v2));
    // std::copy_if(std::make_move_iterator(begin(v1)),
    //          std::make_move_iterator(end(v1)),
    //          std::back_inserter(v2),
    //          [](const Foo& v){ return v.index <= 17; });

    // this line DOESN'T COMPILE 
    v1.erase(v1.begin(), it); // copy assignment operator is implicitly deleted because 'Foo' has a
      user-declared move constructor

    // if I don't loop on auto& the for loop will call copy constructor
    for (auto const& e : v1) {
        std::cout << "v1: " << e.name << " " << e.index << std::endl;
    }
    for (auto const& e : v2) {
        std::cout << "v2: " << e.name << " " << e.index << std::endl;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-09-22 19:40:00

您需要检查的是three/five/zero的规则。

你需要一个复制赋值操作符来编译你的代码:

代码语言:javascript
复制
Foo& operator=(const Foo& other) {
    this->name = other.name;
    this->index = other.index;
    std::cout << "Calling copy assignment " << other.name << other.index << std::endl;
    return *this;
}

以及符合规则5的移动赋值运算符。

godbolt上直播

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58048789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档