如何修复 std::stoi 中的"std::invalid_argument"
How to fix "std::invalid_argument" in std::stoi
我正在尝试做一个套接字服务器,但是当我尝试传递数字时,我得到了std::invalid_argument
,我不知道为什么。我重新制作了代码以使其更简单,如下所示:
std::string a = std::to_string(32);
char texto[5];
strcat(texto,a.c_str());
printf("%s", texto);//Isn't printing a string that you can convert to a number
std::string b = texto;
int s = std::stoi(texto);
std::cout << s;
我希望输出 32,但我收到错误。
char texto[5];
strcat(texto,a.c_str());
texto
未初始化,因此它包含垃圾值。 strcat
必须首先阅读它们以查找 NUL 终止符,从而为您提供未定义的行为。
您可能正在寻找strcpy
而不是strcat
.
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- 使用 std::sort 时"invalid operands to binary expression"
- 我越来越"invalid conversion from `void*` to `std::complex<double>*"
- 我在检查字符串时得到"Invalid utf 8 error",但当我使用 std::cout 时似乎是正确的
- "Invalid iterator range"将initializer_list插入 std::vector 时
- GCC vs. Clang:"invalid use of incomplete type" std::d eclval 和模板专业化
- stoi"在抛出'std::invalid argument'实例后终止调用
- Debug assert "Invalid format directive" for std::p ut_time
- std::chrono::time_point invalid value