As far as I know we should never compare two const character strings using relational operators <>
... because the fact that it compares the addresses rather than the values:
const char* sz1 = "Hello";
const char* sz2 = "hello";
if(sz1 < sz2);// not valid. So use strcmp instead.
- What I've noticed that Ordered Associative Containers like
map, multimap, set, multiset
impose a restriction on theirkey
so that the key should some how be compared to order the elements in the container. The default operator for the key is<
operator.
Everything is clear until I've created a map, set
of const char*
then I get the results incorrect:
std::set<const char*> scp{ "Hello", "World", "C++", "Programming" };
std::set<std::string> sstr{ "Hello", "World", "C++", "Programming" };
// error
std::copy(scp.cbegin(), scp.cend(), std::ostream_iterator<const char*>(std::cout, " "));
std::cout << std::endl;
// Ok
std::copy(sstr.cbegin(), sstr.cend(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
It is apparent that
scp
compares pointers to character strings whilesstr
is Ok as long as classstring
has defined<
to work properly.Why STL allows this? (creating associative containers whose key element type is a
char*
) and why here's even no warning?