Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I was wondering how to use find(itr,itr,key) with a map. This code generates an error message with me.
C++
#include <iostream>
#include <string>
#include <algorithm>
#include <map>

using namespace std;

int main()
{
    map<string, string> student_roster;
    student_roster.insert(pair<string,string>("John","a"));
    map<string, string>::iterator itr;
    itr = find( student_roster.begin() , student_roster.end() , "John" );//the error
    if (itr == student_roster.end())
        cerr << "John is not found" << endl;
    else
        cout << "Student : " << itr->first << " Grade : " << itr->second << endl;
}

However, is it not legal to use that algorithm ?

Thanks, Sam.
Posted
Updated 8-Jul-15 3:51am
v3
Comments
Suvendu Shekhar Giri 8-Jul-15 4:22am    
What is the exact error message?
Samuel Shokry 8-Jul-15 4:32am    
That's it , I guess :
e:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|135|error: no match for 'operator==' in '__first.std::_Rb_tree_iterator<_Tp>::operator*<std::pair<const std::basic_string<char="">, std::basic_string<char> > >() == __val'|
Samuel Shokry 8-Jul-15 9:43am    
Oh thanks, but what about this ?
"itr = find( student_roster.begin() , student_roster.end() , "John" );"
I'm just curious about this line, is it not legal in C++ ?
Richard MacCutchan 8-Jul-15 13:58pm    
Yes it's legal C++, but it does not conform to the definition of find, as shown in the documentation. You cannot expect to call functions/methods with random parameters that the method has no knowledge of.

You are using std::find[^]. This is a generic algorithm that works for any container, but it has no knowledge of the properties of the elements of the container. The elements of a map are (key,value) pairs, not strings. Therefore, find fails to compare the elements with the string you pass as the search value. It would need an operator to compare a map element type to string, which doesn't exist.
 
Share this answer
 
Comments
[no name] 9-Jul-15 4:03am    
Not the problem at all. The key and value can both be std::string and there is an equality operator for those. Try it.
Stefan_Lang 9-Jul-15 8:12am    
Yes it is the problem. The element type of a map<string,string> is pair<string,string> , and there is no comparison operator for the operands pair<string,string> and string.

I did implement a short test function, and the compiler pointed me to the following internal template function with exactly the error message reported by the OP:
// TEMPLATE FUNCTION find
template<class _InIt,
class _Ty> inline
_InIt _Find(_InIt _First, _InIt _Last, const _Ty& _Val)
{ // find first matching _Val
for (; _First != _Last; ++_First)
if (*_First == _Val) // <-- error message points to this line!
break;
return (_First);
}
[no name] 9-Jul-15 8:40am    
Thanks for putting us on track. Yes that is the source of error in the supplied code and the reason - which is why we have std::map::find. My apologies.
Samuel Shokry 9-Jul-15 21:24pm    
Thank you so much Stefan, I have got it now.
You are not using find correctly. Please see below

C++
itr = student_roster.find( "John" );
 
Share this answer
 
Comments
Samuel Shokry 8-Jul-15 4:30am    
I know how to use this about what something like that in my code, it works with vectors by the way.
[no name] 8-Jul-15 4:53am    
Just replace your line with this. What's the question? All of this is very clear in the documentation: http://www.cplusplus.com/reference/map/map/find/
Samuel Shokry 8-Jul-15 9:42am    
Have you read the code ? Do you know why this line of code doesn't work ?
"itr = find( student_roster.begin() , student_roster.end() , "John" );"
Of course, I know the usage of the member function find of the container map, I'm just curious about the line that I have written above in this comment.
is it not legal in C++ ?
Richard MacCutchan 8-Jul-15 10:42am    
Go to the link I gave you and read the documentation for the find method.
[no name] 9-Jul-15 8:41am    
Stefan_Lang has given the explanation for the error and this is the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900