C++ 促进多索引私有成员访问
我有一个结构C++ 促进多索引私有成员访问,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,我有一个结构 struct employee { int id; std::string name; employee(int id,const std::string& name):id(id),name(name){} bool operator<(const employee& e)const{return id<e.id;} getId(){return id;} getName(){return name;} }
struct employee
{
int id;
std::string name;
employee(int id,const std::string& name):id(id),name(name){}
bool operator<(const employee& e)const{return id<e.id;}
getId(){return id;}
getName(){return name;}
};
struct employee
{
int-id;
std::字符串名;
雇员(int-id,const-std::string&name):id(id),name(name){}
布尔算子雇员集;
但是,如果我将employee struct的成员设置为私有,那么我将无法使用em作为容器的键。我尝试将指针放在getter函数上,例如&employy::getName
,但它没有解决问题
所以我的问题是:如何使用私有成员作为多索引容器的密钥?有许多密钥提取器可供使用。请在文档中查找预定义的密钥提取器: 您可以使用
mem\u fun
或const\u mem\u fun
来代替member
#include <string>
class employee {
int id;
std::string name;
public:
employee(int id, const std::string &name) : id(id), name(name) {}
bool operator<(const employee &e) const { return id < e.id; }
int getId() const { return id; }
std::string getName() const { return name; }
};
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
namespace bmi = boost::multi_index;
typedef bmi::multi_index_container<
employee,
bmi::indexed_by<
bmi::ordered_unique<bmi::identity<employee> >,
bmi::ordered_non_unique<bmi::const_mem_fun<employee, std::string, &employee::getName> >
> > employee_set;
int main() {}
#包括
班级员工{
int-id;
std::字符串名;
公众:
雇员(int-id,const-std::string&name):id(id),name(name){}
布尔运算符>员工集合;
int main(){}
您将如何向员工
的成员公开使用它的任何其他内容?我可以添加setter-getter函数
#include <string>
class employee {
int id;
std::string name;
public:
employee(int id, const std::string &name) : id(id), name(name) {}
bool operator<(const employee &e) const { return id < e.id; }
int getId() const { return id; }
std::string getName() const { return name; }
};
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
namespace bmi = boost::multi_index;
typedef bmi::multi_index_container<
employee,
bmi::indexed_by<
bmi::ordered_unique<bmi::identity<employee> >,
bmi::ordered_non_unique<bmi::const_mem_fun<employee, std::string, &employee::getName> >
> > employee_set;
int main() {}