首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >const_iterator、find_if和bind2nd:没有匹配的错误调用

const_iterator、find_if和bind2nd:没有匹配的错误调用
EN

Stack Overflow用户
提问于 2014-04-09 12:28:20
回答 2查看 413关注 0票数 0

我尝试使用find_if来根据其值在地图中找到一个键。但我不能编译代码:

代码语言:javascript
复制
struct IsCurrency : binary_function<pair<const Bill::CodeCurrency, string>, string, bool> {
    bool isCurrency(const pair<const Bill::CodeCurrency, string>& element, const string& expected) const {
        return element.second == expected;
    }
};

string currency = "RUB";
map<Bill::CodeCurrency, string>::const_iterator my_currency = find_if(Bill::currency_code_map().begin(), Bill::currency_code_map().end(), bind2nd(IsCurrency(), currency));  /// <--- ERROR IS HERE

Bill::CodeCurrency是一个枚举。

错误:

代码语言:javascript
复制
/usr/include/c++/4.7/bits/stl_algo.h:4490:41:   required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const Bill::CodeCurrency, std::basic_string<char> > >; _Predicate = std::binder2nd<IsCurrency>]’
../src/money_acceptor/itl_bill_acceptor.cpp:182:121:   required from here
/usr/include/c++/4.7/backward/binders.h:155:29: error: no match for call to ‘(const IsCurrency) (const first_argument_type&, const second_argument_type&)’

你能帮我确定这里的错误是什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-09 13:12:31

正如我在注释中所描述的那样,实际的问题是,提供给find_if的谓词必须有一个operator(),而不是一些名称类似于类名的函数。

C++03版本:

代码语言:javascript
复制
#include <map>
#include <functional>
#include <string>
#include <algorithm>

namespace Bill
{
    enum CodeCurrency
    {
        A, B, C
    };

    typedef std::map<CodeCurrency, std::string> currency_code_map_t;
    currency_code_map_t const& currency_code_map()
    {
        static currency_code_map_t m;
        return m;
    }
}

struct IsCurrency
    : std::binary_function< Bill::currency_code_map_t::value_type, std::string,
                            bool >
{
    bool operator()(Bill::currency_code_map_t::value_type const& element,
                    std::string const& expected) const
    {
        return element.second == expected;
    }
};

int main()
{
    std::string currency = "RUB";
    Bill::currency_code_map_t::const_iterator my_currency =
        std::find_if( Bill::currency_code_map().begin(),
                      Bill::currency_code_map().end(),
                      bind2nd(IsCurrency(), currency) );
}

C++11版本:

代码语言:javascript
复制
#include <map>
#include <functional>
#include <string>
#include <algorithm>

namespace Bill
{
    enum CodeCurrency
    {
        A, B, C
    };

    typedef std::map<CodeCurrency, std::string> currency_code_map_t;
    currency_code_map_t const& currency_code_map()
    {
        static currency_code_map_t m;
        return m;
    }
}

int main()
{
    std::string currency = "RUB";
    auto check = [&currency](Bill::currency_code_map_t::value_type const& element)
                 { return element.second == currency; };
    auto my_currency =
        std::find_if( Bill::currency_code_map().cbegin(),
                      Bill::currency_code_map().cend(),
                      check );
}

注意,这个算法是O(N)。如果需要经常查找元素(可以是O( boost::bimap )),您可能需要考虑使用类似于boost::bimap的东西。

票数 0
EN

Stack Overflow用户

发布于 2014-04-09 12:47:52

bool isCurrency(...)替换为bool operator () (...),以使您的结构可调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22962551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档