C++ 如何在c+中检测转换是否失败+;0x
我尝试执行字符串到int的转换,如下所示:C++ 如何在c+中检测转换是否失败+;0x,c++,type-conversion,C++,Type Conversion,我尝试执行字符串到int的转换,如下所示: #include<iostream> #include<sstream> #include<string> int main(int argc, char ** argv) { int i; char *str = "12a3"; std::stringstream ss; ss << str; ss >> i; std::cout <&
#include<iostream>
#include<sstream>
#include<string>
int main(int argc, char ** argv)
{
int i;
char *str = "12a3";
std::stringstream ss;
ss << str;
ss >> i;
std::cout << i << std::endl; //prints 12
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
int i;
char*str=“12a3”;
std::stringstream-ss;
ss>i;
std::cout当操作员>
发现无法使用的结果时,它会在流上设置failbit
,流本身将评估为false
:
if(!(ss >> i)) { // or ss >> i; if(!ss) or if(ss.fail())
// the conversion failed, do whatever you need to here.
ss.clear(); // reset the fail flag so you can read from the stream again
// you can use ss.ignore() to skip bad input if you want to move on to other parts
}
当操作员>
发现无法使用的结果时,会在流上设置failbit
,流本身将评估为false
:
if(!(ss >> i)) { // or ss >> i; if(!ss) or if(ss.fail())
// the conversion failed, do whatever you need to here.
ss.clear(); // reset the fail flag so you can read from the stream again
// you can use ss.ignore() to skip bad input if you want to move on to other parts
}
当操作员>
发现无法使用的结果时,会在流上设置failbit
,流本身将评估为false
:
if(!(ss >> i)) { // or ss >> i; if(!ss) or if(ss.fail())
// the conversion failed, do whatever you need to here.
ss.clear(); // reset the fail flag so you can read from the stream again
// you can use ss.ignore() to skip bad input if you want to move on to other parts
}
当操作员>
发现无法使用的结果时,会在流上设置failbit
,流本身将评估为false
:
if(!(ss >> i)) { // or ss >> i; if(!ss) or if(ss.fail())
// the conversion failed, do whatever you need to here.
ss.clear(); // reset the fail flag so you can read from the stream again
// you can use ss.ignore() to skip bad input if you want to move on to other parts
}
这是正确的行为。将>
转换为int
的操作成功,这是正确的
你必须问另一个问题。你真正想做的测试是:“在我成功地读取了int
之后,流中是否还有剩余的字符尚未处理?”
当你这样做的时候,考虑一下cin
int x, y;
std::cin >> x;
std::cin >> y;
它从标准输入中尽可能多地读取到x
,在第一个非数字处停止。然后,如果第一个数字后面只有一些空格,则它将尝试读取空格后面的数字,并将该数字存储在y
中
所有输入流(无论是cin
还是一个实例stringstream
)都应包含许多字符串/数字/任何内容。每个>
只需(成功地)读取尽可能多的内容即可。这是正确的行为。将
转换为int
的操作成功,这是正确的
你必须问另一个问题。你真正想做的测试是:“在我成功地读取了int
之后,流中是否还有剩余的字符尚未处理?”
当你这样做的时候,考虑一下cin
int x, y;
std::cin >> x;
std::cin >> y;
它从标准输入中尽可能多地读取到x
,在第一个非数字处停止。然后,如果第一个数字后面只有一些空格,则它将尝试读取空格后面的数字,并将该数字存储在y
中
所有输入流(无论是cin
还是一个实例stringstream
)都应包含许多字符串/数字/任何内容。每个>
只需(成功地)读取尽可能多的内容即可。这是正确的行为。将
转换为int
的操作成功,这是正确的
你必须问另一个问题。你真正想做的测试是:“在我成功地读取了int
之后,流中是否还有剩余的字符尚未处理?”
当你这样做的时候,考虑一下cin
int x, y;
std::cin >> x;
std::cin >> y;
它从标准输入中尽可能多地读取到x
,在第一个非数字处停止。然后,如果第一个数字后面只有一些空格,则它将尝试读取空格后面的数字,并将该数字存储在y
中
所有输入流(无论是cin
还是一个实例stringstream
)都应包含许多字符串/数字/任何内容。每个>
只需(成功地)读取尽可能多的内容即可。这是正确的行为。将
转换为int
的操作成功,这是正确的
你必须问另一个问题。你真正想做的测试是:“在我成功地读取了int
之后,流中是否还有剩余的字符尚未处理?”
当你这样做的时候,考虑一下cin
int x, y;
std::cin >> x;
std::cin >> y;
它从标准输入中尽可能多地读取到x
,在第一个非数字处停止。然后,如果第一个数字后面只有一些空格,则它将尝试读取空格后面的数字,并将该数字存储在y
中
所有的输入流(无论是cin
还是一个实例stringstream
)都应该包含许多字符串/数字/任何内容。每个>
都只是尽可能地(成功地)读取数据。我所知的最简单的方法
int main(int argc, char ** argv)
{
std::string num;
long i;
char *str = "12a3";
char * endp;
std::stringstream ss;
ss << str;
ss >> num;
i = strtol(num.c_str(), // string to convert as a C style string,
&endp, //pointer to be updated with character that ended the conversion
10); //base of number conversion
if (*endp != '\0')
{
//didn't read whole string. conversion failed
}
std::cout << i << std::endl; //prints 12
}
int main(int argc,char**argv)
{
std::string num;
龙我;
char*str=“12a3”;
char*endp;
std::stringstream-ss;
ss>num;
i=strtol(num.c_str(),//要转换为c样式字符串的字符串,
&endp,//要用结束转换的字符更新的指针
10) ;//数字转换的基数
如果(*endp!='\0')
{
//未读取整个字符串。转换失败
}
我所知道的最简单的方法
int main(int argc, char ** argv)
{
std::string num;
long i;
char *str = "12a3";
char * endp;
std::stringstream ss;
ss << str;
ss >> num;
i = strtol(num.c_str(), // string to convert as a C style string,
&endp, //pointer to be updated with character that ended the conversion
10); //base of number conversion
if (*endp != '\0')
{
//didn't read whole string. conversion failed
}
std::cout << i << std::endl; //prints 12
}
int main(int argc,char**argv)
{
std::string num;
龙我;
char*str=“12a3”;
char*endp;
std::stringstream-ss;
ss>num;
i=strtol(num.c_str(),//要转换为c样式字符串的字符串,
&endp,//要用结束转换的字符更新的指针
10) ;//数字转换的基数
如果(*endp!='\0')
{
//未读取整个字符串。转换失败
}
我所知道的最简单的方法
int main(int argc, char ** argv)
{
std::string num;
long i;
char *str = "12a3";
char * endp;
std::stringstream ss;
ss << str;
ss >> num;
i = strtol(num.c_str(), // string to convert as a C style string,
&endp, //pointer to be updated with character that ended the conversion
10); //base of number conversion
if (*endp != '\0')
{
//didn't read whole string. conversion failed
}
std::cout << i << std::endl; //prints 12
}
int main(int argc,char**argv)
{
std::string num;
龙我;
char*str=“12a3”;
char*endp;
std::stringstream-ss;
ss>num;
i=strtol(num.c_str(),//要转换为c样式字符串的字符串,
&endp,//要用结束转换的字符更新的指针
10) ;//数字转换的基数
如果(*endp!='\0')
{
//我没有读wh