std::unordered_map<std::pair<Gdiplus::Color, float>, std::shared_ptr<Gdiplus::Pen>> mymap;
input = std::make_pair(color, width);
當我嘗試做Unordered.map插入問題
mymap.insert(std::make_pair(color,width), pen);
我得到 C2664:「的std :: _ List_iterator < _Mylist>的std :: _哈希< _Traits> ::插入& >(STD :: _ List_const_iterator < _Mylist>,_ Valty):標準::對< _Ty1,_Ty2>不能從轉換參數1 '' 到 '的std :: _ List_const_iterator < _Mylist>' 1>
我試過
mymap.insert(std::make_pair(input,pen))
但這也給我一個錯誤。我不明白爲什麼,因爲輸入是一對顏色和浮
我加入整個代碼,因爲我仍然得到問題
std::unordered_map<std::pair<Gdiplus::Color, float>, std::shared_ptr<Gdiplus::Pen>> mymap;
std::shared_ptr<Gdiplus::Pen> getPen(Gdiplus::Color const & color, float width)
{
std::pair<Gdiplus::Color,float> input;
input = std::make_pair(color, width);
std::unordered_map<std::pair<Gdiplus::Color, float>, std::shared_ptr<Gdiplus::Pen>>::const_iterator got = mymap.find (input);
if (got == mymap.end())
{
auto pen = std::make_shared<Gdiplus::Pen>();
pen->SetColor(color);
pen->SetWidth(width);
//std::pair<Gdiplus::Color,float> input2;
mymap.insert(std::make_pair(input, pen));
return pen;
}
else
{
if (std::shared_ptr<Gdiplus::Pen> m_pen = got->second)
return m_Pen;
}
}
};
error C2440: 'type cast' : cannot convert from 'const std::pair<_Ty1,_Ty2>' to 'size_t' with
[
_Ty1=Gdiplus::Color,
_Ty2=float
]
什麼是'Gdiplus :: Color'?它是一個枚舉嗎?否則,可能會丟失散列函數。 std :: map工作嗎? – balki