Convert string to int with bool/fail in C++(在 C++ 中使用 bool/fail 将字符串转换为 int)
问题描述
我有一个 std::string,它可以是一个字符串,也可以是一个值(例如 0).
I have a std::string which could be a string or could be a value (such as 0).
将 std::string 转换为具有失败能力的 int 的最佳或最简单的方法是什么?我想要 C# 的 Int32.TryParse 的 C++ 版本.
What is the best or easiest way to convert the std::string to int with the ability to fail? I want a C++ version of C#'s Int32.TryParse.
推荐答案
使用 boost::lexical_cast.如果无法完成转换,它将抛出异常.
Use boost::lexical_cast. If the cast cannot be done, it will 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(...)
    {
        /* ... */
    }
}
<小时>
没有提升:
Without 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(...)
    {
        /* ... */
    }
}
<小时>
虚假提升:
Faking boost:
#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 提供无抛出版本),比如这个:
If you want no-throw versions of these functions, you'll have to catch the appropriate exceptions (I don't think boost::lexical_cast provides a no-throw version), something like this:
#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;
    }   
}
                        这篇关于在 C++ 中使用 bool/fail 将字符串转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中使用 bool/fail 将字符串转换为 int
				
        
 
            
        基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 - 如何在 C++ 中初始化静态常量成员? 2022-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 这个宏可以转换成函数吗? 2022-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 常量变量在标题中不起作用 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				