std :: find_if如何使用指针列表?

时间:2016-03-09 13:55:47

标签: if-statement find predicate

请考虑以下代码块:

1- //.h
     std::list<Item> itemsList;

.............

 //.cpp

  2-   struct ItemExist: public std::binary_function< Item, int, bool > 
            {
              bool operator () ( const Item&sc, const int &number ) const 
                {
                return sc.orderNumber == number;
                }
             };

.....

3-    std::list<Item>::iterator itd= std::find_if(itemList.begin(),itemList.end(),std::bind2nd( ItemExist(),orderNumber ) );

...... Item是一个名为orderNumber

的属性的类

1 ^ block:声明一个项目列表

2 ^ block:定义了一个Predicate类

3 ^ block:在列表中搜索给定的项目。

一切正常。

网上钓鱼我使用指向项目的指针列表,我应该将声明更改为块1,如下所示:

 std::list<Item*> itemsList; 

搜索块-3变为:

 std::list<Item*>::iterator itd= std::find_if(itemList.begin(),itemList.end(),std::bind2nd( ItemExist(),orderNumber ) );

此时我遇到了(我认为)谓词类的问题。编译器说:

[C++ Error] _algobase.c(183): E2064 Cannot initialize 'const Item&' with 'Item *'
[C++ Error] _algobase.c(183): E2342 Type mismatch in parameter '__x' (wanted 'const Item&', got 'Item *')

所以,问题是:我应该如何修复谓词(块2)以使其全部工作? 我无法弄明白。请帮帮我。

1 个答案:

答案 0 :(得分:0)

我的尝试是:

int

它似乎有效,但我不确定这是否是最严格的解决方案。