首页 > 编程语言 > 详细

C++输入输出

时间:2015-04-02 13:04:05      阅读:162      评论:0      收藏:0      [点我收藏+]

最近看到真么一段代码,就重温了一下C++的输入输出流。代码如下:

#include <iostream>
#include <string>
#include <sstream>

std::string convertToString(double x)
{
    std::ostringstream o;
    if( o << x)
    {
        return o.str();
    }
    
    return "conversion error";
} 

double convertToDouble(const std::string &str)
{
    std::istringstream i(str);
    double x;
    if( i >> x)
    {
        return x;
    }
    
    return 0.0;
    
}

int main()
{
    char b[10];
    std::string a;
    sprintf(b, "%d", 1975);
    a = b;
    std::cout << a << std::endl;
    
    std::string cc = convertToString(1976);
    std::cout << cc << std::endl;
    
    std::string dd = "2016";
    
    int p = convertToDouble(dd) + 2;
    std::cout << p << std::endl;
    
    return 0;
}

 

C++输入输出

原文:http://www.cnblogs.com/OrdinaryMiracle/p/4386463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!