에 필요한 핵심으로 qux
를 사용해야하는 경우가 필요, 그래야 작동합니다.
boost::multi_index::member<Foo::Bar, int, &Foo::Bar::qux>
아래의 사용자 의견을 바탕으로 Boost.MultiIndex 튜토리얼의 basic example을 사용 사례와 일치하도록 수정했습니다. 원래의 예는이 struct
/* an employee record holds its ID, name and age */
struct employee
{
int id;
std::string name;
int age;
employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
// ...
};
그리고 multi_index
컨테이너 지금
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<
tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
ordered_non_unique<
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
ordered_non_unique<
tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
> employee_set;
으로 정의된다, 우리가 employee::name
실제로 중첩 struct Bar
의 일원이되도록 employee
을 수정할 수 및 employee
포함 말할 다음이 포함되어 Bar
의 두 인스턴스
struct employee
{
int id;
struct Bar
{
Bar(std::string name) : name(name) {}
std::string name;
} bar1, bar2;
int age;
// ...
};
하지만 당신은 직접 중첩 struct
의 데이터 멤버를 표시하기 위해이 선언
ordered_non_unique<
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>
을 수정할 수 없습니다. 대신, 우리는 employee::Bar
오브젝트를 주문하는 방법이 필요합니다
ordered_non_unique<
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,employee::Bar,bar1)>
에 선언을 수정, 그래서에 색인 경우, 이러한 변화와 함께 그 정의
struct Bar
{
// ...
bool operator<(Bar const& other) const { return name < other.name; }
};
에 비교 연산자를 추가해야 컨테이너 name
태그를 사용하는 경우 bar1.name
데이터 멤버를 기반으로 주문할 것입니다.
여기에 complete working example이 있습니다.
또한 bar1.name
에 포함 된 문자의 역순으로 포함되도록 bar2.name
을 초기화하고 name2
태그를 사용하여 색인을 생성하는 옵션을 추가했습니다.
왜이 질문을 다시합니까? 나는 정확히 한 시간 전에 똑같은 질문을 보았다. –
@Bot 커뮤니티 회원이 방금 편집했을 때 다시 묻는 이유는 무엇입니까? ** [image] (http://i.imgur.com/dGv8orA.png ) ** _ ('활동 순위'질문을 보지 않으려면 '최신 질문'탭을 계속 사용하십시오 ._) 덧글을 주셔서 감사합니다 – sehe