【发布时间】:2021-11-06 16:44:35
【问题描述】:
我试图找到两个字符串的最大值,它在第一种情况下给出了正确的答案(传递std::string 变量时),但在第二种情况下给出了错误(传递直接字符串时)。
#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;
}
【问题讨论】:
-
必须链接到Why should I not #include <bits/stdc++.h>?。但如果你还是使用它,并与
using namespace std;、things can get really weird结合使用。 -
您是要查找最长的字符串还是按字典顺序查找后面的字符串?
标签: c++ algorithm max stdstring c++-standard-library