I am trying to find the maximum of two strings and it is giving me the correct answer in the first case (when passing std::string
variables) but giving an error in the second case (when passing direct strings).
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Case 1
string str1 = "abc", str2 = "abcd";
cout << max(str1, str2) << endl;
// Case 2
cout << max("abc", "abcd") << endl;
}
std::string literals are Standard Library implementations of user-defined literals (see below) that are represented as "xyz"s (with a s suffix). This kind of string literal produces a temporary object of type std::string , std::wstring , std::u32string , or std::u16string , depending on the prefix that is specified.
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.
std::string actually maintains the size as one of its data member.
In your second case,
std::cout << std::max("abc", "abcd") << std::endl;
they are string literals, in which "abc"
has type char const [4]
and "abcd"
has type char const [5]
.
Therefore, in the function call std::max("abc", "abcd")
, the std::max
has to deduce
auto max(char const (&a)[4], char const (&b)[5]) {
return a < b ? b : a;
}
This is not possible as the std::max
has no function template overload, which takes different types as template arguments. Hence, the error!
If you explicitly mention the template type const char*
in std::max
, this could have been compiled. This is because, for "abc"
and "abcd"
the type can be also be const char*
s due to array to pointer decay in C++.
std::cout << std::max<const char*>("abc", "abcd" ) << '\n'; // compiles
^^^^^^^^^^^^^
In addition, the std::initializer_list
overload of the std::max
, in turn will deduce the above also const char*
as template type:
std::cout << std::max({ "abc", "abcd" }) << '\n'; // compiles
However, you should not be doing it !
As @AlanBirtles pointed out, this can cause the undefined behavior, due to the fact that the std::max
will compare the pointers of the two different arrays. The result can not be relayed and should be doing the above. Use the std::string
for comparison as in your first case. Using string literals (since C++14), you can do a minimal change, and make the second case, same as first one:
#include <string>
using namespace std::string_literals;
std::cout << std::max("abc"s, "abcd"s) << '\n';
As a side note, see the followings:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With