在创建堆栈数据结构时使用 stoi 会导致错误
using stoi when creating a stack data structure results in errors
我已经创建了一个堆栈数据结构并正在实施它。我一直在使用下面的功能,
void Stack::solution(const char *input_path, const char *output_path)
{
string line;
Stack tempStack;
ifstream myfile(input_path);
ofstream outfile(output_path);
while (getline(myfile, line)) {
if (line.substr(0,1) == "s") {
Stack tempStack = Stack();
};
if (line.substr(0,1) == "e") {
int a = stoi(line.substr(1,1));
tempStack.push(a);
};
if (line.substr(0,1) == "o") {
int a = stoi(line.substr(1,1));
int num = tempStack.pop();
string x = to_string(num);
outfile << x << "";
};
}
}
但问题是这部分代码,除非不是:
if (line.substr(0,1) == "o") {
int a = stoi(line.substr(1,1));
int num = tempStack.pop();
string x = to_string(num);
outfile << x << "";
};
我收到此错误:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6
我最初认为问题在于我没有将 int num
转换为 string
,但即使我这样做了,错误也没有得到解决。
您没有显示您的输入实际是什么样子,但如果它与 this question 中显示的输入有任何相似之处,那么 o
案例没有与之关联的整数值,因此对 line.substr(1,1)
的调用不会 return 一个 std::stoi()
可以转换为 int
的 string
,因此抛出 std::invalid_argument
异常的原因.这是有道理的,因为 o
情况意味着只从堆栈中弹出一个值,而不是将一个新值压入堆栈。因此,只需完全删除对 std::stoi()
的调用:
if (line.substr(0,1) == "o") {
//int a = stoi(line.substr(1,1)); // <-- get rid of this!
int num = tempStack.pop();
string x = to_string(num);
outfile << x;
};
此外,您在 s
案例的范围内有 2 个 tempStack
对象。您正在创建第二个 Stack
对象,它是 shadowing 现有的 Stack
对象,这不是您想要发生的。此代码:
Stack tempStack;
...
if (line.substr(0,1) == "s") {
Stack tempStack = Stack();
};
应该是这样的:
Stack tempStack;
...
if (line.substr(0,1) == "s") {
tempStack = Stack();
};
因此,话虽如此,您的函数应该看起来更像这样:
void Stack::solution(const char *input_path, const char *output_path)
{
string line;
Stack tempStack;
ifstream myfile(input_path);
ofstream outfile(output_path);
while (getline(myfile, line)) {
if (line.substr(0,1) == "s") {
tempStack = Stack();
}
if (line.substr(0,1) == "e") {
int a = stoi(line.substr(1,1));
tempStack.push(a);
}
if (line.substr(0,1) == "o") {
int num = tempStack.pop();
string x = to_string(num);
outfile << x;
}
}
}
我已经创建了一个堆栈数据结构并正在实施它。我一直在使用下面的功能,
void Stack::solution(const char *input_path, const char *output_path)
{
string line;
Stack tempStack;
ifstream myfile(input_path);
ofstream outfile(output_path);
while (getline(myfile, line)) {
if (line.substr(0,1) == "s") {
Stack tempStack = Stack();
};
if (line.substr(0,1) == "e") {
int a = stoi(line.substr(1,1));
tempStack.push(a);
};
if (line.substr(0,1) == "o") {
int a = stoi(line.substr(1,1));
int num = tempStack.pop();
string x = to_string(num);
outfile << x << "";
};
}
}
但问题是这部分代码,除非不是:
if (line.substr(0,1) == "o") {
int a = stoi(line.substr(1,1));
int num = tempStack.pop();
string x = to_string(num);
outfile << x << "";
};
我收到此错误:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6
我最初认为问题在于我没有将 int num
转换为 string
,但即使我这样做了,错误也没有得到解决。
您没有显示您的输入实际是什么样子,但如果它与 this question 中显示的输入有任何相似之处,那么 o
案例没有与之关联的整数值,因此对 line.substr(1,1)
的调用不会 return 一个 std::stoi()
可以转换为 int
的 string
,因此抛出 std::invalid_argument
异常的原因.这是有道理的,因为 o
情况意味着只从堆栈中弹出一个值,而不是将一个新值压入堆栈。因此,只需完全删除对 std::stoi()
的调用:
if (line.substr(0,1) == "o") {
//int a = stoi(line.substr(1,1)); // <-- get rid of this!
int num = tempStack.pop();
string x = to_string(num);
outfile << x;
};
此外,您在 s
案例的范围内有 2 个 tempStack
对象。您正在创建第二个 Stack
对象,它是 shadowing 现有的 Stack
对象,这不是您想要发生的。此代码:
Stack tempStack;
...
if (line.substr(0,1) == "s") {
Stack tempStack = Stack();
};
应该是这样的:
Stack tempStack;
...
if (line.substr(0,1) == "s") {
tempStack = Stack();
};
因此,话虽如此,您的函数应该看起来更像这样:
void Stack::solution(const char *input_path, const char *output_path)
{
string line;
Stack tempStack;
ifstream myfile(input_path);
ofstream outfile(output_path);
while (getline(myfile, line)) {
if (line.substr(0,1) == "s") {
tempStack = Stack();
}
if (line.substr(0,1) == "e") {
int a = stoi(line.substr(1,1));
tempStack.push(a);
}
if (line.substr(0,1) == "o") {
int num = tempStack.pop();
string x = to_string(num);
outfile << x;
}
}
}