测试一下这地方如何?

 

时过境迁,很多C ++ 的基础知识有些生疏了,这会拿来重温一下一上来就发现输入输出好多忘了,想想前段时间拿CString和std::string混用来实字符串转换,感觉比较可笑,其实C ++ 中很轻松就能实现转换.

一、作用域和字符串格式化的例子,后面会不断补充

#include 
< Windows.h >
#include 
< sstream >
#include 
< iostream >
#include 
< iomanip >
#include 
< string >
using namespace std;
class  A
{
public:
    A()
{
        cout
<<"构造函数"<<endl;
    }

    
~A()
    
{
        cout
<<"析构函数"<<endl;
    }

    A(
int m, int d):month(m),day(d){};
    
int getMonth()
    
{
        
return month;
    }

    
int getDay()
    
{
        
return day;
    }

protected:
private:
    
int month;
    
int day;
}
;

int  _tmain( int  argc, _TCHAR *  argv[])
{

    
if (1)
    
{
        A a;
    }

    
//上面的a此时已经脱离作用域了,所以发生了析构
    A b(9,12);
    ostringstream f;
    f.fill(
'0');
    f
<<setw(2)<<b.getMonth()<<"-"<<setw(2)<<b.getDay();

    
char buf[100];
    cin
>>buf;
    cout
<<"buf:="<<buf<<endl;
    f
<<endl<<setw(4)<<strlen(buf)<<buf<<endl; 
    string str 
= f.str();
    cout
<<str<<endl;
    cout
<<"end of main"<<endl;
    
//b则在退出之前发生析构
 return 0;
}


 
 
itpub得blog速度太慢了,看看这里如何呢? 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值