我有一个std::string
,它可以是一个字符串,也可以是一个值(比如0
)。
将std::string
转换为具有故障能力的int
的最佳或最简单的方法是什么?我想要一个C#的Int32.TryParse
的C++版本。
发布于 2009-08-07 07:31:29
使用boost::lexical_cast。如果无法完成强制转换,它将执行throw an exception。
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
int main(void)
{
std::string s;
std::cin >> s;
try
{
int i = boost::lexical_cast<int>(s);
/* ... */
}
catch(...)
{
/* ... */
}
}
不使用boost:
#include <iostream>
#include <sstream>
#include <string>
int main(void)
{
std::string s;
std::cin >> s;
try
{
std::stringstream ss(s);
int i;
if ((ss >> i).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast();
}
/* ... */
}
catch(...)
{
/* ... */
}
}
伪装助推:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
T lexical_cast(const std::string& s)
{
std::stringstream ss(s);
T result;
if ((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast();
}
return result;
}
int main(void)
{
std::string s;
std::cin >> s;
try
{
int i = lexical_cast<int>(s);
/* ... */
}
catch(...)
{
/* ... */
}
}
如果你想要这些函数的无抛出版本,你必须捕获适当的异常(我不认为boost::lexical_cast
提供了无抛出版本),如下所示:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
T lexical_cast(const std::string& s)
{
std::stringstream ss(s);
T result;
if ((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast();
}
return result;
}
template <typename T>
bool lexical_cast(const std::string& s, T& t)
{
try
{
// code-reuse! you could wrap
// boost::lexical_cast up like
// this as well
t = lexical_cast<T>(s);
return true;
}
catch (const std::bad_cast& e)
{
return false;
}
}
int main(void)
{
std::string s;
std::cin >> s;
int i;
if (!lexical_cast(s, i))
{
std::cout << "Bad cast." << std::endl;
}
}
发布于 2009-08-07 08:10:07
使用streams的其他答案将成功,即使字符串在有效数字后包含无效字符,例如"123abc“。我不熟悉boost,所以不能评论它的行为。
如果您想知道字符串是否包含数字并且只包含数字,则必须使用strtol:
#include <iostream>
#include <string>
int main(void)
{
std::string s;
std::cin >> s;
char *end;
long i = strtol( s.c_str(), &end, 10 );
if ( *end == '\0' )
{
// Success
}
else
{
// Failure
}
}
strtol返回一个指向结束解析的字符的指针,因此可以很容易地检查是否解析了整个字符串。
请注意,strtol返回的是long类型,而不是int类型,但根据编译器的不同,它们可能是相同的。在标准库中没有strtoi函数,只有atoi函数,它不返回解析结束字符。
发布于 2015-10-09 20:16:59
异常不应用于布尔型测试
被接受的答案确实是一个糟糕的问题答案,因为它违反了“在例外情况下使用异常”的原则。
异常是处理异常情况的一个很好的工具--真正出了问题的情况。对于现有的用例来说,它们是糟糕的工具。部分是因为抛出和捕获异常的代价很高,部分是因为它是误导性的代码--当开发人员看到异常时,他们应该能够合理地认为那里出了问题。关于这个基本原则的很好的讨论比比皆是,但我喜欢“务实的程序员”的讨论,或者说这还不错:http://www.lohmy.de/2013/03/06/writing-use-cases-exception-or-alternate-flow/
如果您总是期望一个数字,请使用boost::lexical_cast
在接收非数字的情况下,boost::lexical_cast是一个最优的解决方案。
如果非数字是您用例的一部分,则使用boost::try_lexical_convert
如果你正在处理一个字符串,如果它是一个数字,你想做一件事,如果它是一个数字,你想做另一件事,不要在布尔测试中使用异常。这只是一个糟糕的编程。
事实上,boost提供了try_lexical_convert,它在lexical_cast的实现中使用(取自这里的文档:http://www.boost.org/doc/libs/1_58_0/doc/html/boost_lexical_cast/synopsis.html#boost_lexical_cast.synopsis.lexical_cast)。
template <typename Target, typename Source>
inline Target lexical_cast(const Source &arg)
{
Target result;
if (!conversion::try_lexical_convert(arg, result))
throw bad_lexical_cast();
return result;
}
https://stackoverflow.com/questions/1243428
复制相似问题