key_struct kStruct;
kStruct.x = 2;
std::find_if(m_cueCommands.begin(), m_cueCommands.end(), find_MyInt(kStruct));
struct key_struct
{
int x;
string y;
string z;
}
struct find_myInt : std::unary_function<key_struct, bool>
{
int x;
find_myInt(key_struct kStruct):myInt(kStruct.x){}
bool operator()(key_struct const& m) const
{
return m.x == myInt;
}
};
我很確定我有這個有點搞砸了,但是,我認爲它是適度接近。我想要做的是將key_struct作爲我地圖中的關鍵字。我希望能夠搜索密鑰並返回設置key_struct的信息。如果我將key_struct設置爲x = 2,那麼我只想返回int x等於2的地方。如果我設置x = 2和y =「testString」的位置,那麼我只想返回其中x = 2和y =「testString」。C++ STD :: find_if使用結構謂詞
我相信我的方法很接近,但我認爲我錯過了一些東西。想法?
這適用於'binary_search'或'lower_bound',但不適用於執行線性搜索的'std :: find_if'。 – 2011-04-19 13:44:53
'find_if'需要一個等式關係,而不是一個嚴格的弱排序。 – aschepler 2011-04-19 13:46:29
請注意,OP說:「我想要做的是將key_struct作爲我地圖中的關鍵字。」 'map'需要嚴格的弱排序。 – 2011-04-19 13:47:48