boost multi_index-如何将一个容器中的元素添加到另一个容器中,如果其类型为仅移动? [英] boost multi_index - how to add an element from one container to another if its type is move-only?

查看:57
本文介绍了boost multi_index-如何将一个容器中的元素添加到另一个容器中,如果其类型为仅移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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;

以及此MyContainerType的两个容器:

And two containers of this MyContainerType:

MyContainerType c1, c2;

在某个时刻,我想遍历所有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. 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).

在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);
}

这会将奇数项从 source 移到 other :

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

这篇关于boost multi_index-如何将一个容器中的元素添加到另一个容器中,如果其类型为仅移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆