当KEY为boost ::可选参数

时间:2015-05-28 05:46:30

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

#include <iostream>
using namespace std;

#include <typeinfo>

#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 <boost/optional.hpp>

using boost::multi_index_container;
namespace mi = boost::multi_index;

class employee
{
    public :

    std::string name;

    boost::optional<unsigned int> id;

    unsigned int presence;
};

struct ByName{};

struct ById{};


int main() 
{
    typedef boost::multi_index_container<
      employee,
      mi::indexed_by<
    // sort by less<string> on name
    mi::ordered_unique<mi::tag<ByName>,mi::member<employee,std::string,&employee::name>>,
    mi::ordered_unique<mi::tag<ById>,mi::member<employee,boost::optional<unsigned int>,&employee::id>>
      > 
    >employee_set;

    employee_set empDataBase;

    class employee emp1, emp2, emp3;

    std::string name("Mahesh");
    emp1.name = name;
    emp1.presence = true;
    emp1.id = 1000;

    std::string name1("Rajesh");
    emp2.name = name1;
    emp2.presence = true;


    std::string name3("Piku");
    emp3.name = name3;
    emp3.presence = true;
    emp3.id = 2000;

    bool result = empDataBase.insert(emp1).second;
    printf("result 1 is %d\n", result);

    result = empDataBase.insert(emp2).second;
    printf("result 2 is %d\n", result);

    result = empDataBase.insert(emp3).second;
    printf("result 3 is %d\n", result);

    /* Get the value from the multi-index using first and second ordered index */
    employee_set::iterator i = empDataBase.get<ByName>().find(name1);

    printf ("name - %s, presence - %d, Id intialized ? %d\n", (*i).name.c_str(), (*i).presence, (*i).id.is_initialized());


    employee_set::iterator ii = empDataBase.get<ById>().find(2000);

    printf ("name - %s, presence - %d, id Value %d\n", (*ii).name.c_str(), (*ii).presence, (*ii).id.get());

    empDataBase.erase(i);
    empDataBase.erase(ii);

    /* Get the value from the multi-index using first and second ordered index */
    i = empDataBase.get<ByName>().find(name1);

    printf ("name - %s, presence - %d, Id intialized ? %d\n", (*i).name.c_str(), (*i).presence, (*i).id.is_initialized());

    return 0;

}

employee_set::iterator ii给了我以下错误。我该如何定义迭代器?

  

prog.cpp:在函数&#39; int main()&#39;:   prog.cpp:76:63:错误:转换为&#39; boost :: multi_index :: detail :: ordered_index,&amp; employee :: id&gt;,   std :: less&gt;,   boost :: multi_index :: detail :: nth_layer&lt; 2,员工,   提高:: multi_index ::的indexed_by,   提高:: multi_index ::成员,   &安培;雇员::名称&gt; &gt;中   提高:: multi_index :: ordered_unique,   提高:: multi_index ::成员,   &安培;雇员:: ID&GT; &GT; &gt;,std :: allocator&gt;,   boost :: mpl :: v_item,0&gt;,   boost :: multi_index :: detail :: ordered_unique_tag&gt; :: iterator {aka   boost :: multi_index :: detail :: bidir_node_iterator&gt; &GT; &GT;}&#39;到非标量类型   &#39;的boost :: multi_index :: multi_index_container的,   提高:: multi_index ::成员,   &安培;雇员::名称&gt; &gt;中   提高:: multi_index :: ordered_unique,   提高:: multi_index ::成员,   &安培;雇员:: ID&GT; &GT; &GT; &gt; :: iterator {aka   boost :: multi_index :: detail :: bidir_node_iterator&gt; &GT; &GT; &GT;}&#39;要求   employee_set :: iterator ii = empDataBase.get()。find(2000);

1 个答案:

答案 0 :(得分:4)

multi_index_container的每个索引都有自己的iterator类型,不能与其他索引的索引自由互换。所以,你必须写一些类似

的东西
employee_set::index<ById>::type::iterator ii = empDataBase.get<ById>().find(2000);

或者,如果使用C ++ 11,只需

auto ii = empDataBase.get<ById>().find(2000);

理解ii的类型与i的类型不同。同样,行

empDataBase.erase(ii);

将无效,因为iiById索引的迭代器,而empDataBase,进一步限定,则引用索引#0(ByName)。所以你必须写

empDataBase.get<ById>().erase(ii);

或诉诸iterator projection