简体   繁体   中英

Unordered.map insert issue with pair keys

std::unordered_map<std::pair<Gdiplus::Color, float>, std::shared_ptr<Gdiplus::Pen>> mymap;
input = std::make_pair(color, width);

When i try to do

mymap.insert(std::make_pair(color,width), pen);

I get C2664: 'std::_List_iterator<_Mylist> std::_Hash<_Traits>::insert&>(std::_List_const_iterator<_Mylist>,_Valty)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::_List_const_iterator<_Mylist>' 1>

i tried doing

mymap.insert(std::make_pair(input,pen)) 

but that also gives me an error. i don't understand why since input is a pair of color and float

I'm adding the whole code because i'm still getting issues

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
      ]

Use emplace(key, value) or insert(pair) .

mymap.emplace(std::make_pair(color, width), pen);
mymap.insert(std::make_pair(std::make_pair(color, width), pen));

insert(it, pair) takes an iterator as its first argument (as an hint for performance reasons), not a key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM