조금 혼란 스럽습니다. 당신은이 같은 구조를 가지고 말하는 것 :
(psudocode)
struct Gizmo
{
Gizmo(int foo, string bar) : foo_(foo), bar_(bar) {};
int foo_;
string bar_;
};
Gizmo make_gizmo(int foo, string bar) { return Gizmo(foo,bar); }
std::map<string, Gizmo> my_gizmos;
my_gizmos["aaa"] = make_gizmo(1,"hello");
my_gizmos["bbb"] = make_gizmo(2,"there");
... 그리고 당신이 foo_
의 값으로 Gizmo
의를 검색 할 수 있도록하려면?
두 가지 주요 옵션이 있습니다.
1) 그냥 자신 펑 사용자 정의 (다시 psudocude) 쓰기 :
struct match_foo : public std::unary_function<...>
{
match_foo(int foo) : foo_(foo) {};
bool operator()(map<string,Gizmo>::const_iterator it) const
{
return it->second.foo_ == foo_;
}
private:
int foo_;
};
map<string,Gizmo>::const_iterator that = find_if(my_gizmos.begin(), my_gizmos.end(), match_foo(2));
};
2) foo_
값의 인덱스를 만들기를, 다시 메인 map
의 Gizmo
매핑. 이지도는 당신이 주요지도, my_gizmos
를 업데이트 할 어떤 시간을 유지하는 것이 같은 ...
map<int,map<string,Gizmo>::const_iterator> foo_index;
을 ... 보일 수 있습니다.
본질적으로 답을 주셨습니다. 직접 작성하십시오. –
부스트에 문제가 있습니까? – AJG85
std :: pair을 사용하고 검색을 위해 자신의 술어를 추가 할 수 있습니다. 같은 개체를 가리키는 두 개의 다른지도를 가질 수 있습니다. 어쩌면 문자열과 int를 하나의 키로 해시 할 수 있습니까? –
ManicQin