S
S S
Hi
I have a very basic question, but it's a good one. Below is the code
fragment.
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
int main()
{
const int N = 6;
const char* a[N] = {"isomer", "ephemeral", "prosaic",
"nugatory", "artichoke", "serif"};
set<const char*, ltstr> A(a, a + N);
if (A.find("ephemeral") != A.end())
cout << "Found";
else
cout << "Not found";
return 0;
}
Output will be -> "Found"
My question is , Why?
How it is able to compare a const char* with another const char* to
find that value? I did not specify any equality operator? I just
mentioned strcmp(s1, s2) < 0
which means, when strcmp(s1, s2) == 0 (in case of match)
it will return false. So how set/map are able to find the const char*
value?
I have a very basic question, but it's a good one. Below is the code
fragment.
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
int main()
{
const int N = 6;
const char* a[N] = {"isomer", "ephemeral", "prosaic",
"nugatory", "artichoke", "serif"};
set<const char*, ltstr> A(a, a + N);
if (A.find("ephemeral") != A.end())
cout << "Found";
else
cout << "Not found";
return 0;
}
Output will be -> "Found"
My question is , Why?
How it is able to compare a const char* with another const char* to
find that value? I did not specify any equality operator? I just
mentioned strcmp(s1, s2) < 0
which means, when strcmp(s1, s2) == 0 (in case of match)
it will return false. So how set/map are able to find the const char*
value?