时过境迁,很多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;
}
一、作用域和字符串格式化的例子,后面会不断补充
#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;
}