简体   繁体   中英

OOP iterator and find method not working correctly

Doctor Hospital::findDoctor(const Doctor &doctor) {
    map<int, Doctor>::iterator iter = find(hospitalInner->doctors.begin(), hospitalInner->doctors.end(), 5);
}

Hy, I'm having some problem with this exact method. Maybe someone know where is the problem? (I'm adding current error.)

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::pair' (or there is no acceptable conversion) C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include\\xutility 3026 1 hospital5

To find the element with a particular key, use the member find function (rather than the std::find algorithm):

map<int, Doctor>::iterator iter = hospitalInner->doctors.find(5);

You could achieve this with std::find , but you would have to write a custom comparator that checks the first member of each std::pair in the map (a std::map stores its keys and values as std::pair s).

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