string和CString风格的不同

1 view
Skip to first unread message

bruc...@gmail.com

unread,
Sep 28, 2005, 1:50:41 AM9/28/05
to 星星爱CPP

/*
问:[coolfreezing]
个人认为C++的string功能没有MFC的CString强大。不知是我学艺不精还是确实如此。
尤其是在拼接字符串时,没有%d, %s用着那么方便。
当然我说的是C++,scanf之类的是C里的吧。
C++中如何能方便的处理string的拼接呢?还望指点一二,多谢!

答:[周星星]
对于string和CString的比较,其实不是STL和MFC的比较,也不是C++和C的比较,它其实是过程调用和面向对象的区别。
过程调用和面向对象各有优缺点,你喜欢用printf还是cout?
使得string区别于CString的原因有两点:
1.
风格统一:stl希望能统一以面向对象的手段解决问题。对于自定义类型,CString就没有办法处理了,对于string而言,可以重载。
2.
粒度大小:stl希望能降低模块的粒度以提高复用性,所以string只管字符串,而字符串流由stringstream处理。
*/

#include <iostream>
#include <sstream>
using namespace std;

int main( void )
{
ostringstream os;
os << "ABC" << "\t" << 123 << "\t" << boolalpha << true;
string s = os.str();
cout << s << endl;

istringstream is( s );
string a;
int b;
bool c;
is >> a >> b >> boolalpha >> c;
cout << a<< "\t" << b << "\t" << boolalpha << c << endl;

return 0;
}

---

Reply all
Reply to author
Forward
0 new messages