gpt4 book ai didi

c++ - std::istream 到无符号数值,如何检测负值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:23 26 4
gpt4 key购买 nike

我正在开发一个模板函数,将字符串转换为数字,以便在没有 C++11 的情况下编译一些遗留代码。

函数是:

template<typename T>
void checkValid( const std::string& val )
{
std::stringstream str1;
T temp1;

str1 << val;
str1 >> temp1;
if ( str1.fail() || str1.bad() )
std::cout << "error, " << val << " is not a valid string value" << std::endl;
else
std::cout << "ok, " << val << " is converted to " << temp1 << std::endl;
}

它工作得很好,除了负值:

// valid integer
checkValid<int>( "3" );
// valid integer
checkValid<int>( "-1000" );
// invalid integer
checkValid<int>( "foo" );
// invalid integer out of range (>INT_MAX)
checkValid<int>( "8393930300303" );
// invalid integer out of range (<INT_MIN)
checkValid<int>( "-8393930300303" );

// valid unsigned integer
checkValid<unsigned int>( "3" );
// invalid unsigned integer
checkValid<unsigned int>( "foo" );
// unsigned integer out of range (>UINT_MAX)
checkValid<unsigned int>( "8393930300303" );
// unsigned integer out of range (<0)
checkValid<unsigned int>( "-3" );

这个输出:

ok, 3 is converted to 3
ok, -1000 is converted to -1000
error, foo is not a valid string value
error, 8393930300303 is not a valid string value
error, -8393930300303 is not a valid string value
ok, 3 is converted to 3
error, foo is not a valid string value
error, 8393930300303 is not a valid string value
ok, -3 is converted to 4294967293

虽然我希望最后一行:

error, -3 is not a valid string value

当目标类型为无符号时,负字符串值无法正确处理。修复 checkValid 并使其对所有类型(有符号、无符号数字以及 float / double )都按预期运行的最佳策略是什么?

最佳答案

对于无符号类型,流允许接受负数。它的机制与

unsigned type foo = -some_value

由于它们可以接受负数,因此流永远不会失败,并且您将具有将负数分配给无符号类型的正常行为。

我们可以在您的函数中为此添加检查。对于类型 T , T() - T(1) < 0 , 只有在类型有符号时才为真,否则减法会回绕并成为最大值 T可以代表。因此,如果我们检查该条件,并且字符串以 '-' 开头,那么您就知道它不是“有效”值。这让你的功能看起来像

template<typename T>
void checkValid( const std::string& val )
{
std::stringstream str1;
T temp1;

str1 << val;
str1 >> temp1;
if ( str1.fail() || str1.bad() || (!(T() - T(1) < T()) && val[0] == '-') )
std::cout << "error, " << val << " is not a valid string value" << std::endl;
else
std::cout << "ok, " << val << " is converted to " << temp1 << std::endl;
}

如果您的字符串可以有前导空格,那么您需要替换 val[0] == '-'检查类似 val[val.find_first_not_of(" ")] == '-' 的内容

关于c++ - std::istream 到无符号数值,如何检测负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com