问题描述
我正在 DFA 上编写这个项目,我想将保存为字符串的整数的每个数字保存并转换为 int 数组.这是负责此操作的函数的代码:
I'm writing this project on DFA and i want to save and covert each digit of an integer saved as a string to an int array.This is the code from the function responsible for that:
int l=final_states.size();
int* temp_final;
temp_final=new int [l];
for(int i=0;i<l;i++)
{
temp_final[i]=atoi(final_states.at(i).c_str());
}
这给了我以下错误:request for member 'c_str' in '((DFA*)this)->DFA::final_states.std::basic_string<_CharT, _Traits, _Alloc>::at<char, std::char_traits, std::allocator>(((std::basic_string<char>::size_type)i))',属于非类类型'char'|
.
所以如果你能告诉我如何进行这种转换和保存工作,那就太好了.
So if you could tell me how to make this conversion and saving work , that would be great.
推荐答案
atoi()
函数需要一个 const char*
,你不能调用 .c_str()
与 .at(i)
的结果实际上是一个 char&
值.
The atoi()
function wants a const char*
, you cannot call .c_str()
with the result of .at(i)
which is actually a char&
value.
只需将您的分配行修改为
Just fix your assignment line to
temp_final[i] = int(final_states[i]) - int('0');
虽然你也可以检查你是否真的有一个数字 那里,然后将其放入结果数组中:
Though you also might check if you really have a digit there, before putting it into your result array:
if(std::isdigit(final_states[i])) {
temp_final[i] = int(final_states[i]) - int('0');
}
else {
// Skip, or throw error ...
}
这篇关于将字符串中保存的数字中的每个数字转换为 int 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!