gpt4 book ai didi

c++ - 当 KEY 为 boost::optional 参数时用于 boost 多索引的迭代器

转载 作者:太空狗 更新时间:2023-10-29 20:57:30 24 4
gpt4 key购买 nike

#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: In function 'int main()': prog.cpp:76:63: error: conversion from 'boost::multi_index::detail::ordered_index, &employee::id>, std::less >, boost::multi_index::detail::nth_layer<2, employee, boost::multi_index::indexed_by, boost::multi_index::member, &employee::name> >, boost::multi_index::ordered_unique, boost::multi_index::member, &employee::id> > >, std::allocator >, boost::mpl::v_item, 0>, boost::multi_index::detail::ordered_unique_tag>::iterator {aka boost::multi_index::detail::bidir_node_iterator > > >}' to non-scalar type 'boost::multi_index::multi_index_container, boost::multi_index::member, &employee::name> >, boost::multi_index::ordered_unique, boost::multi_index::member, &employee::id> > > >::iterator {aka boost::multi_index::detail::bidir_node_iterator > > > >}' requested employee_set::iterator ii = empDataBase.get().find(2000);

最佳答案

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 (名字)。所以你必须写

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

或求助于iterator projection .

关于c++ - 当 KEY 为 boost::optional 参数时用于 boost 多索引的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30498132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com