我该如何解决它?libc++abi.dylib:以 std::invalid_argument 类型的未捕获异常终止:s
How can I fix it? libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
#define REP(i,a,b) for(int i = a; i < b; i++)
#define FOR(i,n) REP(i,0,n)
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
using namespace std;
下面的函数是通过字符串方法总结两个大整数的函数。输入可以大于长整型的限制。
string string_add(string a, string b) {
string new_str = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string long_s;
string short_s;
if (a.size() >= b.size()) {
long_s = a;
short_s = b;
}
else{
long_s = b;
short_s = a;
}
int carry = 0;
int dif = long_s.size() - short_s.size();
FOR(i, dif)
short_s += "0";
FOR(i, long_s.size()) {
int hab = stoi(long_s.substr(i, 1)) + stoi(short_s.substr(i, 1)) + carry;
if (hab > 9) {
carry = 1;
hab -= 10;
}
else carry = 0;
new_str += to_string(hab);
}
if (carry != 0) new_str += to_string(carry);
reverse(new_str.begin(), new_str.end());
return new_str;
}
下面是主要部分。
int main() {
while(!cin.eof()){
int n;
string dp[251];
cin >> n;
dp[0] = 1;
dp[1] = 1;
REP(i,2,n)
dp[i] = string_add(string_add(dp[i-2], dp[i-2]), dp[i-1]);
cout << dp[n];
}
return 0;
}
问题
如何修复此错误?我认为问题是"stoi"的输入。但我不知道如何解决它。请帮忙。
来自 cpp首选项:
例外:std::invalid_argument 如果无法执行转换
看起来您正在操作的字符串之一无法转换为整数,因此stoi
引发异常。 一般来说,了解你正在调用的函数的所有失败模式是个好主意——你需要通过在程序逻辑中的某个地方捕获此异常来处理这种情况(或者保证发送到stoi
的字符串始终是可转换的,这在某些用例中可能是可能的)。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 表示"accepting anything for this template argument" C++概念的通配符
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- "could not convert template argument"到自己的类型