C++ stoi : none of the 2 overloads could convert all the argument types

标签 c++ string c++11

我正在编写一个与字符串相关的练习:输入一个字符串(无论是 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/

相关文章:

C++11:不带复制构造函数的自定义基于范围的循环

C++ 解析函数类型模板参数

c++ - 无法将文件路径从 C++ 保存到数据库

c++ - 如何在 C++ 中大写/小写 UTF-8 字符?

C++ - 谷歌protobuf

javascript - 如何将 json 字符串中的某些文本设为粗体?

php - 如果数据为空,特定的加密算法如何工作

c++ - 哪个构造函数将触发 move 语义?

c++ - Windows 7 异常代码 : 0xc0000409

c++ - Ogre3D 显示段错误