boost :: multi_index_container:检索end iterator时出现问题

时间:2015-11-09 20:08:23

标签: c++ boost iterator boost-multi-index

我有一个Color类。

我想创建一个boost::multi_index_container,它使用颜色ID和颜色位置(它在渐变类中)作为键。

为了使代码更易读,我已在我的IndexedColorSet类中封装了boost实现。

我想检索按其位置排序的颜色。为了做到这一点,我创建了一个getStartEndPositionIterators方法,它返回按位置排序的multi_index_container的开始和结束迭代器。

我的问题是结束迭代器似乎无法正常工作。如果我创建一个从开始到结束打印所有迭代器位置的循环,它们会正确打印,但是当我到达结束迭代器时,它会无限期地在最后存储的值和无效值之间循环。

如何检索右端迭代器?我做错了什么?

使用boost 1.58.0和gcc 5.2.1。

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <utility>
#include <iostream>

//////////////////////////////////////////////////////////////
/// Class that must be inserted into multi_index_container. //
//////////////////////////////////////////////////////////////

class Color {
public:
    double red;
    double green;
    double blue;
    double alpha;
};

//////////////////////////////////////////////////////
/// Class that implements the multi_index_container //
/// using ordering by id and by position.           //
//////////////////////////////////////////////////////

class IndexedColorSet {
public:

    // Class that wil be used as multi_index_container template parameter.
    class IndexedColor {
    public:
        int id;
        double position;
        Color color;
        IndexedColor() {}
        IndexedColor(int id, double position, const Color &color) :
            id(id), position(position), color(color) {}
    };

    // typedef for multi_index_container iterator
    typedef ::boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<IndexedColor, std::allocator<IndexedColor> > > > PositionIterator;

public:

    void insertColor(int id, double position, const Color &color);
    // Retrieve begin and end iterator using position as index
    std::pair<const PositionIterator&, const PositionIterator&> getStartEndPositionIterators() const;

private:

    // Tags
    struct id{};
    struct position{};

    // Container creation.
    // It creates an ordered_unique index using id
    // and a ordered_not_unique index using position
    typedef ::boost::multi_index_container<
        IndexedColor,
        ::boost::multi_index::indexed_by<
            ::boost::multi_index::ordered_unique<
                ::boost::multi_index::tag<id>,
                BOOST_MULTI_INDEX_MEMBER(IndexedColor,int,id)
            >,
            ::boost::multi_index::ordered_non_unique<
                ::boost::multi_index::tag<position>,
                BOOST_MULTI_INDEX_MEMBER(IndexedColor,double,position)
            >
        >
    > PrivateIndexedColorSet;

private:

    PrivateIndexedColorSet m_set;
};

// Insert a color associated to id and position.
void IndexedColorSet::insertColor(int id, double position, const Color &color) {
    m_set.insert(IndexedColor(id, position, color));
}

// Retrieve a std::pair containing begin and end iterator if multi_index_container
// using the position as key.
std::pair<const IndexedColorSet::PositionIterator&, const IndexedColorSet::PositionIterator&> IndexedColorSet::getStartEndPositionIterators() const {
    const auto& positionSet = ::boost::multi_index::get<position>(m_set);
    const auto& beginIt = positionSet.begin();
    const auto& endIt = positionSet.end();
    return std::pair<const PositionIterator&, const PositionIterator&>(beginIt, endIt);
}

int main()
{
    IndexedColorSet set;

    // Populate the set
    int id1 = 1;
    int id2 = 2;
    int id3 = 3;
    double position1 = 0.4;
    double position2 = 0.6;
    double position3 = 0.3;
    Color color1{0.1, 0.3, 0.4, 0.5};
    Color color2{0.2, 0.4, 0.5, 0.6};
    Color color3{0.3, 0.4, 0.5, 0.6};
    set.insertColor(id1, position1, color1);
    set.insertColor(id2, position2, color2);
    set.insertColor(id3, position3, color3);

    // Retrieve ordered position iterators
    auto iterators = set.getStartEndPositionIterators();

    // Testing that are ordered
    // I should obtain
    // 0.3
    // 0.4
    // 0.6
    //
    // Instead I obtain
    // 0.3
    // 0.4
    // 0.6
    // 0
    // 0.6
    // 0
    // 0.6
    // 0
    // 0.6
    // 0... and so on
    for (auto it = iterators.first; it != iterators.second; ++it) {
        std::cout << it->position << std::endl;
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

当你想要返回迭代器本身时,问题是getStartEndPositionIterators将一对或引用返回给迭代器(引用引用getStartEndPositionIterators内的对象变为无效的一旦成员函数退出):

std::pair<PositionIterator,PositionIterator> getStartEndPositionIterators() const;

我已经对您的代码进行了一些清理:特别是,您获取PositionIterator类型的方式绝对没有记录,您可以采用合法方式执行此操作,如下所示。

<强> Live On Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <utility>
#include <iostream>

//////////////////////////////////////////////////////////////
/// Class that must be inserted into multi_index_container. //
//////////////////////////////////////////////////////////////

class Color {
public:
    double red;
    double green;
    double blue;
    double alpha;
};

//////////////////////////////////////////////////////
/// Class that implements the multi_index_container //
/// using ordering by id and by position.           //
//////////////////////////////////////////////////////

class IndexedColorSet {
public:

    // Class that wil be used as multi_index_container template parameter.
    class IndexedColor {
    public:
        int id;
        double position;
        Color color;
        IndexedColor() {}
        IndexedColor(int id, double position, const Color &color) :
            id(id), position(position), color(color) {}
    };

private:

    // Tags
    struct id{};
    struct position{};

    // Container creation.
    // It creates an ordered_unique index using id
    // and a ordered_not_unique index using position
    typedef ::boost::multi_index_container<
        IndexedColor,
        ::boost::multi_index::indexed_by<
            ::boost::multi_index::ordered_unique<
                ::boost::multi_index::tag<id>,
                BOOST_MULTI_INDEX_MEMBER(IndexedColor,int,id)
            >,
            ::boost::multi_index::ordered_non_unique<
                ::boost::multi_index::tag<position>,
                BOOST_MULTI_INDEX_MEMBER(IndexedColor,double,position)
            >
        >
    > PrivateIndexedColorSet;

public:

    void insertColor(int id, double position, const Color &color);
    // Retrieve begin and end iterator using position as index
    typedef PrivateIndexedColorSet::index<position>::type::iterator PositionIterator;
    std::pair<PositionIterator,PositionIterator> getStartEndPositionIterators() const;

private:

    PrivateIndexedColorSet m_set;
};

// Insert a color associated to id and position.
void IndexedColorSet::insertColor(int id, double position, const Color &color) {
    m_set.insert(IndexedColor(id, position, color));
}

// Retrieve a std::pair containing begin and end iterator if multi_index_container
// using the position as key.
std::pair<
  IndexedColorSet::PositionIterator,
  IndexedColorSet::PositionIterator>
IndexedColorSet::getStartEndPositionIterators() const {
  return std::make_pair(
      m_set.get<position>().begin(),
      m_set.get<position>().end());
}

int main()
{
    IndexedColorSet set;

    // Populate the set
    int id1 = 1;
    int id2 = 2;
    int id3 = 3;
    double position1 = 0.4;
    double position2 = 0.6;
    double position3 = 0.3;
    Color color1{0.1, 0.3, 0.4, 0.5};
    Color color2{0.2, 0.4, 0.5, 0.6};
    Color color3{0.3, 0.4, 0.5, 0.6};
    set.insertColor(id1, position1, color1);
    set.insertColor(id2, position2, color2);
    set.insertColor(id3, position3, color3);

    // Retrieve ordered position iterators
    auto iterators = set.getStartEndPositionIterators();

    // Testing that are ordered
    // I should obtain
    // 0.3
    // 0.4
    // 0.6
    for (auto it = iterators.first; it != iterators.second; ++it) {
        std::cout << it->position << std::endl;
    }
    return 0;
}

答案 1 :(得分:1)

图书馆作者已经向您展示了主要问题。以下是我喜欢与你一起过去的实时编码清理:)

<强> Live On Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

struct Color {
    double red;
    double green;
    double blue;
    double alpha;
};

namespace bmi = boost::multi_index;

class IndexedColorSet {
  public:

    struct IndexedColor {
        int id;
        double position;
        Color color;
    };
  private:

    typedef bmi::multi_index_container<
        IndexedColor,
        bmi::indexed_by<
            bmi::ordered_unique<     bmi::tag<struct by_id>,       bmi::member<IndexedColor, int,    &IndexedColor::id>       >,
            bmi::ordered_non_unique< bmi::tag<struct by_position>, bmi::member<IndexedColor, double, &IndexedColor::position> >
        >
    > PrivateIndexedColorSet;

  public:
    // typedef for multi_index_container iterator
    using PositionIterator = PrivateIndexedColorSet::index<by_position>::type::const_iterator;
    using PositionRange    = boost::iterator_range<PositionIterator>;

    void insertColor(int id, double position, const Color &color);
    PositionRange getPositionRange() const;

  private:
    PrivateIndexedColorSet m_set;
};

// Insert a value
void IndexedColorSet::insertColor(int id, double position, const Color &color) {
    m_set.insert({id, position, color});
}

// Retrieve the full range by_position
IndexedColorSet::PositionRange IndexedColorSet::getPositionRange() const {
    auto& positionSet = bmi::get<by_position>(m_set);
    auto beginIt      = positionSet.begin();
    auto endIt        = positionSet.end();

    return { beginIt, endIt };
}

int main() {
    IndexedColorSet set;

    // Populate the set
    set.insertColor(1, 0.4, {0.1, 0.3, 0.4, 0.5});
    set.insertColor(2, 0.6, {0.2, 0.4, 0.5, 0.6});
    set.insertColor(3, 0.3, {0.3, 0.4, 0.5, 0.6});

    // Retrieve ordered position iterators
    auto iterators = set.getPositionRange();

    for (auto& ic : iterators) {
        std::cout << ic.position << std::endl;
    }
}

打印

0.3
0.4
0.6