简体   繁体   English

从boost :: multi_index获取值

[英]Get values from a boost::multi_index

I have created a boost::multi_index successfully and inserted values too. 我已成功创建了boost::multi_index并插入了值。 I have two hashed indices to the multi_index. 我有两个散列索引到multi_index。 Both are member functions, but one is unique and the other one is non-unique. 两者都是成员函数,但一个是唯一的,另一个是非唯一的。

I am trying to figure out the way to get the values from the container using the values of the hashes. 我试图找出使用哈希值从容器中获取值的方法。 I could not understand how I should do that. 我无法理解我应该怎么做。 I searched online, and I see there are a lot of people have asked this question. 我在网上搜索,我看到有很多人都问过这个问题。 But I dont understand what needs to be done. 但我不明白需要做些什么。 I saw a few solutions in C++11 but I dont use C++11 and I dont understand whats being done. 我在C ++ 11中看到了一些解决方案,但是我没有使用C ++ 11,我也不明白它是什么。 Can someone please explain to me how to use it? 有人可以向我解释如何使用它吗? Given below is my code, 以下是我的代码,

#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;
}

I want to be able to, 我希望能够,

  1. Get the values non-unique Id maps to 获取非唯一Id映射的值
  2. Get the value (if it exists) that the unique Id maps to 获取唯一Id映射到的值(如果存在)

Please let me know the correct/simplest way to get this done. 请让我知道完成此操作的正确/最简单的方法。 Also I don't use C++11. 我也不使用C ++ 11。

Here's how you'd retrieve from the string-based index: 以下是从基于字符串的索引中检索的方法:

mi_storeMe container;

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

For the ID-based index, it's very similar: 对于基于ID的索引,它非常相似:

mi_storeMe container;

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

The answer uses auto from C++11 so that I can avoid spelling out the types involved. 答案使用来自C ++ 11的auto ,这样我就可以避免拼写出所涉及的类型。 If you do not have access to C++11, please do the type deduction yourself by reading Boost's multi_index documentation. 如果您无法访问C ++ 11,请阅读Boost的multi_index文档,自行进行类型扣除。 I cannot vouch for correctness, but I believe the iterator type can be spelled as 我不能保证正确性,但我相信迭代器类型可以拼写为

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

Tangentially related: how to do printf-style debugging of multi-index containers without C++11. 切线相关:如何在没有C ++ 11的情况下对多索引容器进行printf式调试。

First off, remember thatwhile you don't have auto , you still have type deduction in templates. 首先,请记住,虽然您没有auto ,但您仍然可以在模板中进行类型扣除。 No need to spell out types if a function template can deduce them: 如果函数模板可以推导出它们,则无需拼写出类型:

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

printContainerItems(container.begin(), container.end());

Second, you can easily iterate over an index: 其次,您可以轻松地遍历索引:

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

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

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