【问题标题】:Get values from a boost::multi_index从 boost::multi_index 获取值
【发布时间】:2016-10-19 16:51:38
【问题描述】:

我已成功创建boost::multi_index 并插入了值。我对 multi_index 有两个散列索引。两者都是成员函数,但一个是唯一的,另一个是非唯一的。

我正在尝试找出使用散列值从容器中获取值的方法。我不明白我应该怎么做。我在网上搜索了一下,发现有很多人问过这个问题。但我不明白需要做什么。我在 C++11 中看到了一些解决方案,但我不使用 C++11,而且我不明白正在做什么。有人可以向我解释如何使用它吗?以下是我的代码,

#include "stdafx.h"
#include<multi_index_container.hpp>
#include<boost/multi_index/hashed_index.hpp>
#include<boost/multi_index/mem_fun.hpp>
#include<boost/multi_index/tag.hpp>



class RetClass
{
    int a, b;
};

class StoreMe
{
    RetClass ex;
    std::string exStr;
    int id;
public:
    void setId(RetClass a) 
    {
        ex = a;
    };


    virtual const RetClass& getId() const { return ex; }
    virtual std::string getIdString() const { return exStr; }
    int getUniqueId() const { return id; }
};

struct IndexByStringId{};
struct IndexByUniqueId{};

typedef boost::multi_index_container<
    StoreMe,
    boost::multi_index::indexed_by<
        boost::multi_index::hashed_unique<
            boost::multi_index::tag<IndexByStringId>,
            boost::multi_index::const_mem_fun<StoreMe, std::string,     &StoreMe::getIdString> 
        >,
        boost::multi_index::hashed_non_unique<
            boost::multi_index::tag<IndexByUniqueId>,
            boost::multi_index::const_mem_fun<StoreMe, int,     &StoreMe::getUniqueId> 
        >
    >
> mi_storeMe;

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

我希望能够,

  1. 获取非唯一 ID 映射到的值
  2. 获取唯一 ID 映射到的值(如果存在)

请告诉我完成这项工作的正确/最简单的方法。我也不使用 C++11。

【问题讨论】:

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


    【解决方案1】:

    这是从基于字符串的索引中检索的方法:

    mi_storeMe container;
    
    std::string needle = whatToSearchFor();
    auto iterator = container.get<IndexByStringId>().find(needle);
    if (iterator != container.get<IndexByStringId>().end())
      found(*iterator);
    else
      notFound();
    

    对于基于ID的索引,非常相似:

    mi_storeMe container;
    
    RetClass needle = whatToSearchFor();
    auto range = container.get<IndexByUniqueId>().equal_range(needle);
    processRangeFromTo(range.first, range.second);
    

    答案使用 C++11 中的auto,这样我就可以避免拼写所涉及的类型。如果您无法访问 C++11,请通过阅读 Boost 的 multi_index 文档自己进行类型推导。我不能保证正确性,但我相信迭代器类型可以拼写为

    mi_storeMe::index<IndexByStringId>::type::iterator
    

    切线相关:如何在没有 C++11 的情况下对多索引容器进行 printf 样式的调试。

    首先,请记住,虽然您没有auto,但您仍然可以在模板中进行类型推断。如果函数模板可以推导出类型,则无需拼出类型:

    template <class It>
    void printContainerItems(It from, It to) {
      for (; from != to; ++from)
        print(*from);
    }
    
    printContainerItems(container.begin(), container.end());
    

    其次,您可以轻松地遍历索引:

    const mi_Container::index<IndexByIdString>::type& index = container.get<IndexByIdString>();
    for (
      mi_Container::index<IndexByIdString>::type::iterat‌​or it = index.begin(), end = index.end();
      it != end;
      ++it
    )
    {
      operate_on(*it);
    }
    

    【讨论】:

    • 这样好吗@Agnew? std::pair&lt;mi_Container::index&lt;IndexByIdString&gt;::type::iterator, mi_Container::index&lt;IndexByIdString&gt;::type::iterator&gt; range_it = Containers.get&lt;IndexByIdString&gt;().equal_range(nonEqId); if (range_it.first != Containers.get&lt;IndexByIdString&gt;().end()) //If it was found {//We might have a range of values for (mi_Container::index&lt;IndexByIsString&gt;::type::iterator it = range_it.first; it != range_it.second; it++) { value = *it; //Get the value pointed by the iterator, do what I want on "value" . . .
    • @codeworks 乍一看还可以,但你为什么不问问你的编译器和程序呢? ;-)
    • @Agnew,我已经检查过了。我发布了一个确认,因为 equal_range 确实返回一个空范围(即第一个和最后一个迭代器等于 end())
    • @codeworks 请注意,在您的 Q 中,IndexByStringIdhashed_uniqueIndexByUniqueIdhashed_non_unique。您可能希望在真实代码中仔细检查您的设置,和/或使用整个容器的调试输出(通过任意索引遍历它)以确保它包含您认为的内容。
    • @Agnew,我会检查一下,感谢您指出这一点。我想遍历容器并查看它包含的内容,但我找到了 mi_StoreMe::begin()、mi_StoreMe()::end() 或它但找不到 mi_StoreMer::iterator。我如何遍历整个事情(不使用键列表)。事实上,这是我调试它的第一个想法。请告诉我如何获取存储在 m_StoreMe 中的所有值。谢谢
    猜你喜欢
    • 2016-03-23
    • 2011-03-29
    • 2013-07-14
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多