我正在编写一个与字符串相关的练习:输入一个字符串(无论是 char[] 还是 C+11 字符串,所以我选择了后者)然后在给定字符串中找到最长(具有最多字符)的升序子字符串.我的想法是扫描整个字符串并将 str[i]
与 str[i+1]
进行比较。我用stoi
把每个字符都转成int,长这样
if (stoi(str[i]) < stoi(str[i+1]))
但它反而给了我错误:
error C2665: 'std::stoi': none of the 2 overloads could convert all the argument types
我该如何解决?提前致谢。
最佳答案
std::stoi
将数字的字符串表示形式转换为数字本身:stoi("42")
应该等于 42。您需要的是一个按原样完成的字符到字符比较,没有任何额外的转换:
std::size_t i{};
while(i < str.size() - 1 && str[i] < str[i + 1]) ++i;
关于C++ stoi : none of the 2 overloads could convert all the argument types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51559191/