HB1*_*963 -4 c++ dictionary c++11
我确实在下面定义了struct
struct WayStruct{
double ID;
string Neighbours;
};
Run Code Online (Sandbox Code Playgroud)
并在地图下方
map <double,WayStruct> WayMap;
Run Code Online (Sandbox Code Playgroud)
要使用添加新元素到此地图
WaysFind.ID=999;
WaysFind.Neighbours="test";
WayMap.insert(1234,WaysFind);
Run Code Online (Sandbox Code Playgroud)
但是我无法编译.Dev-C++以错误结束
[Error] no matching function for call to 'std::map<double, WayStruct>::insert(double, WayStruct&)'
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里做错了什么?
当我使用make_pair Dev-C++返回时
In instantiation of 'std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = double; _U2 = int; _T1 = const char; _T2 = WayStruct]':
required from here
111 39 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\bits\stl_pair.h [Error] no matching function for call to 'WayStruct::WayStruct(const int&)'
Run Code Online (Sandbox Code Playgroud)
在文件stl_pair.h中
std::map::insert()您尝试使用过载接受类型的一个参数std::map:value_type是std::pair不是两个.为map插入值的常见方法是:
WayMap.insert( std::make_pair( 1234,WaysFind ) );
Run Code Online (Sandbox Code Playgroud)
对于C++ 11,您可以插入您尝试使用的方式emplace:
WayMap.emplace( 1234, WaysFind );
Run Code Online (Sandbox Code Playgroud)
另外注意,您应该注意使用double键作为可能的问题.