#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 gives me following error. How should i define the iterator ?**********
prog.cpp: In function 'int main()':
prog.cpp:76:63: error: conversion from 'boost::multi_index::detail::ordered_index<boost::multi_index::member<employee, boost::optional<unsigned int>, &employee::id>, std::less<boost::optional<unsigned int> >, boost::multi_index::detail::nth_layer<2, employee, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ByName>, boost::multi_index::member<employee, std::basic_string<char>, &employee::name> >, boost::multi_index::ordered_unique<boost::multi_index::tag<ById>, boost::multi_index::member<employee, boost::optional<unsigned int>, &employee::id> > >, std::allocator<employee> >, boost::mpl::v_item<ById, boost::mpl::vector0<mpl_::na>, 0>, boost::multi_index::detail::ordered_unique_tag>::iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<employee, std::allocator<employee> > > >}' to non-scalar type 'boost::multi_index::multi_index_container<employee, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ByName>, boost::multi_index::member<employee, std::basic_string<char>, &employee::name> >, boost::multi_index::ordered_unique<boost::multi_index::tag<ById>, boost::multi_index::member<employee, boost::optional<unsigned int>, &employee::id> > > >::iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<employee, std::allocator<employee> > > > >}' requested
employee_set::iterator ii = empDataBase.get<ById>().find(2000);
^
Aucun commentaire:
Enregistrer un commentaire