⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lexical_cast.h

📁 一些unix下的c/c++的util包
💻 H
字号:
/**
 * 类型转换模板
 * @file lexical_cast.h
 * @date 2006年11月1日
 * @author 周志武
 * @version 1.0.0: 初始版本
 **/
#ifndef _LEXICAL_CAST_H_
#define _LEXICAL_CAST_H_
#include <string>      
#include <limits>
#include <sstream>          
namespace nlkit
{
template<typename Target, typename Source>
class lexical_stream
{
public:
    lexical_stream()
    {
        m_stream.unsetf(std::ios::skipws);

        if(std::numeric_limits<Target>::is_specialized)
            m_stream.precision(std::numeric_limits<Target>::digits10 + 1);
        else if(std::numeric_limits<Source>::is_specialized)
            m_stream.precision(std::numeric_limits<Source>::digits10 + 1);
    }
    ~lexical_stream()
    {
    }
    bool operator<<(const Source &input)
    {
        return m_stream << input;
    }
    template<typename InputStreamable>
    bool operator>>(InputStreamable &output)
    {
        return m_stream >> output &&
               (m_stream >> std::ws).eof();
    }
    bool operator>>(std::string &output)
    {
        output = m_stream.str();
        return true;
    }
private:
    std::stringstream m_stream;
};

/**
 *  类型转换
 */ 
template<typename Target, typename Source>
Target lexical_cast(Source arg)
{
    lexical_stream<Target, Source> interpreter;
    Target result;
    interpreter << arg;
    interpreter >> result;
    //        if(!(interpreter << arg && interpreter >> result))
    return result;
}
}
#endif  //_LEXICAL_CAST_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -