简体   繁体   English

boost multi_index-如果元素的类型为仅移动,如何将元素从一个容器添加到另一个容器?

[英]boost multi_index - how to add an element from one container to another if its type is move-only?

I have: 我有:

struct foo {
    ...
    std::unique_ptr<Bar> bar; // requires foo to be move-only
    foo() { bar = std::make_unique<Bar>(); }
    foo(foo&&) = default;
};

typedef boost::multi_index_container<
    foo,
    boost::multi_index::indexed_by< 
        boost::multi_index::random_access<>,
        boost::multi_index::hashed_unique<
            boost::multi_index::composite_key<
                foo,
                boost::multi_index::member<X,std::string,&X::zoo>,
            >
        >
    >
> MyContainerType;

And two containers of this MyContainerType: 还有两个MyContainerType容器:

MyContainerType c1, c2;

And at some moment I want to iterate through all c1 elements and add some of them (according to some logics,) to c2. 在某个时刻,我想遍历所有c1元素,并将其中一些(根据某些逻辑)添加到c2中。 I tried: 我试过了:

for (auto it = c1.begin(); it != c1.end(); ++it) {
    if (...some logics... ) {
        c2.push_back(std::move(*it));
    }
}

But it does not compile, same as many another ways I have tried. 但是它不能编译,就像我尝试过的许多其他方法一样。

You can use the trick outlined in that answer: Move element from boost multi_index array (which I linked you to on your previous question). 您可以使用该答案中概述的技巧: 从boost multi_index数组移动元素 (我在上一个问题上将其链接到该数组 )。

Live On Coliru 生活在Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/optional.hpp>
#include <iostream>

// move-only
struct foo {
    foo(int x) : x(x) {}
    foo(foo&&)      = default;
    foo(foo const&) = delete;
    foo& operator=(foo&&)      = default;
    foo& operator=(foo const&) = delete;

    int x;
};

template <typename Container>
void dump(std::ostream& os, Container const& c) { 
    for (auto& r: c) os << r.x << " ";
    os << "\n";
}

static_assert(not std::is_copy_constructible<foo>{}, "foo moveonly");

namespace bmi = boost::multi_index;

using foos = bmi::multi_index_container<foo, bmi::indexed_by<bmi::sequenced<> > >;

int main() {
    foos source, other;

    source.emplace_back(1);
    source.emplace_back(2);
    source.emplace_back(3);
    source.emplace_back(4);
    source.emplace_back(5);
    dump(std::cout << "Source before: ", source);

    for (auto it = source.begin(); it != source.end();) {
        if (it->x % 2) { // odd?
            boost::optional<foo> extracted;

            if (source.modify(it, [&](foo& v) { extracted = std::move(v); })) {
                it = source.erase(it);
            } else {
                ++it;
            }
            other.push_back(std::move(*extracted));
        } else {
            ++it;
        }
    }

    dump(std::cout << "Source after: ", source);
    dump(std::cout << "Other after: ", other);
}

This moves the odd items from source to other : 这会将奇数项从source移动到other

Source before: 1 2 3 4 5 
Source after: 2 4 
Other after: 1 3 5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM