c++ - std::map.insert "could not deduce template argument for..."

标签 c++ stl map

我正在尝试熟悉 STL 库,但我无法理解我的编译错误。我使用编译器错误字符串“无法推断...的模板参数”搜索了其他问题,但没有一个答案似乎适用或相关。

Error 4 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

我正在编写一个简单的解释器来计算一个变量的导数/积分。我想要一个映射,用于将用户的输入与内部控制代码相匹配。 key 是一个 trig(或其他)函数,int 是控制代码。我使用一个单独的头文件来#define 函数,但对于这个例子,我只使用整型文字。我正在使用 Visual Studio:

#include <cstdio>
#include <map>
using namespace std;
int main(int argc, char** argv) {
    map< string, int > functions;
    functions.insert( pair<string, int>("sin", 1) );

    return 0;
}

编辑:

在尝试了 Serge 的(有效的)答案之后:

functions.insert(std::make_pair(std::string("sin"), 1));

我意识到错误并尝试了这个:

pair<string, int> temp = pair<string,int>("cos",2);
functions.insert(temp);

虽然这可能不是最理想的,但它说明了在插入 map 之前没有构建对对象的问题。

最佳答案

确保包含 string header 。

#include <map>
#include <utility>
#include <string>

...

std::map<std::string, int> functions;
functions.insert(std::make_pair(std::string("sin"), 1));

关于c++ - std::map.insert "could not deduce template argument for...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056854/

相关文章:

c++ - STL vector 分配与插入

c++ - STL 列表性能很差

c++ - C++11 中 boost::shared_polymorphic_downcast 的 std 等价物在哪里?

c++ - 如何在 Visual Studio 2013 的解决方案中查找派生类?

c++ - 整数作为 C++ 中的条件 +'s ":? "operator

c++ - 删除 std::list::iterator 不会使迭代器无效并销毁对象吗?

java - C++ 到 Java : searching a collection efficiently

java - 我如何按照创建它的相同顺序解析 map (foreach)(JAVA)

java - 在 Java 中将数据附加到 Map

c++ - 抛出错误 "as ‘p’ 中的包装函数未在此范围内声明”