stoi 会导致超出范围的错误
stoi causes out of range error
我有这段代码,它给了我错误terminating with uncaught exception of type std::out_of_range: stoi: out of range
我已经确定是由线路引起的long ascii = std::stoi(temp_string);
我使用stoi
导致这种情况的方式如何,我该如何解决它?
std::string encode(std::string message){
std::string num_value;
long cipher;
if(message.length() < 20){
for(int i = 0; i < message.length(); ++i){
int temp = (char) message.at(i);
num_value += std::to_string(temp);
}
return num_value;
}
else{
int count = 0;
std::string temp_string;
for(int i = 0; i < message.length(); ++i){
int temp = (int) message.at(i);
temp_string += std::to_string(temp);
count++;
if(count == 20){
count = 0;
//add cipher encrypt
long ascii = std::stoi(temp_string);
//cipher = pow(ascii, 10000);
//add n to cipther encrypt
//add cipherencrypt + n to num_value
//reset temp_string
temp_string += "n";
temp_string = "";
}
}
return num_value;
}
int main(){
std::string s = "Hello World my t's your name aaaaaaaaaaaaa?";
std::cout<<"encoded value : "<< encode(s)<<std::endl;
}
std::stoi 返回一个整数;听起来你的数字对于整数来说太大了。尝试改用 std::stol (见这里)。
此外,如果您打算将代码移植(可用于不同的编译器/平台),请记住整数和长整型没有标准大小。除非标准给出的最小大小足够,否则您可能需要查看使用 intX_t(其中 X 是您想要的大小),如 cstdint 标头中给出的那样。
相关文章:
- "myClock"未在此范围错误中声明
- 包含来自另一个文件的函数会导致范围错误(openFoam)
- Visual C:命名空间范围错误或晦涩功能?
- 在遍历字符串时,为什么我没有超出范围错误,即使我已经超出了它的长度?
- 此范围错误中未声明如何修复
- 尝试声明函数的局部变量,但得到范围错误
- c ++ to_string未在此范围错误 [Windows + Devcpp 环境] 中声明
- 将文件读取到向量的向量,超出范围错误
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- Eratosthenes C 的筛子 - 内存处的范围错误
- 向量:内存处的范围错误
- 测试完美哈希函数时超出范围错误
- 为什么此代码会给出 Vector 超出范围错误
- 在填充 Arduino 草图时出现范围错误
- 如何修复C++中矢量的"下标超出范围"错误?
- CPP-模板范围错误
- 在此范围错误中未声明函数错误
- C 表达式:向量下标出范围错误线:1733
- 无法在好友函数中实例化类?我没有得到在范围错误中声明
- 带有类的嵌套结构.范围错误