不能为类型定义模板使用STL函数

问题描述 投票:0回答:1

我已经为typedef创建了map,但是不能使用find方法。这是代码:

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    typedef std::map<std::string, int> customType;
    customType mod;

    mod["something"] = 2;

    if (mod.find("something"))
    {
        std::cout << "found";
    }
}

对于上面的代码,我收到如下错误:

main.cpp: In function ‘int main()’:
main.cpp:22:17: error: could not convert ‘mod.std::map<_Key, _Tp, _Compare, _Alloc>::find, int, std::less >, std::allocator, int> > >(std::basic_string(((const char*)"something"), std::allocator()))’ from ‘std::map, int>::iterator {aka std::_Rb_tree_iterator, int> >}’ to ‘bool’
     if (mod.find("something"))
         ~~~~~~~~^~~~~~~~~~~~~

我知道我可以不使用std::map而直接使用typedef,但是此代码只是我想在项目中执行的操作的示例。请帮助我找到一种使用find方法的方法。

c++
1个回答
2
投票

这与模板和typedef无关。而是,您在错误地使用find方法。 find的行为描述如下:

© www.soinside.com 2019 - 2024. All rights reserved.