那么多字符串类,我该用哪一个?

Dangyuhang 2009-12-16 11:30:44
STL提供了字符串类,WTL提供了字符串类, Winx提供了字符串类,Boost提供了字符串类......
我疯了,C++最让我抓狂的一点就是做同一件事情通常有着n种选择,这个可能是优点,但对于我这种感慨是看C++的人来说就是噩梦。

大家说说,你们是怎么选择的?在什么时候用哪一个?

我用C++主要在Windows上写桌面应用,怕了MFC,所以从Winx开始,这个小库挺不错的,但就是在很多很基础的库的选择上让我很纠结,string只是其中一个,刚刚还开了另外一个帖子问关于HTTP库的选择。

唉!太纠结了。
...全文
224 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dangyuhang 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 loaden 的回复:]
如果你用WTL或者增加的WINX,CString是最佳选择!
因为他们都依赖ATL,而CString是ATL的模板类。
不需要链接到MFC库的。

如果你除了ATL、WTL、WINX外,还有SDK开发,其实VC的std::string也很不错的。
我现在用std::string。
...
[/Quote]

非常感谢!我也正在这么封装一些常用方法出来用呢,哈哈!目前正在看字符串各种编码的处理。
2009-12-18
  • 打赏
  • 举报
回复
由于经常在 Linux 和 Windows 之间跑来跑去,能满足需要的情况下我一般用 std::string。
alexmayer 2009-12-18
  • 打赏
  • 举报
回复
顶一下老邓!
「已注销」 2009-12-17
  • 打赏
  • 举报
回复
如果你用WTL或者增加的WINX,CString是最佳选择!
因为他们都依赖ATL,而CString是ATL的模板类。
不需要链接到MFC库的。

如果你除了ATL、WTL、WINX外,还有SDK开发,其实VC的std::string也很不错的。
我现在用std::string。

我这样使用,感觉很爽:
namespace qp
{

typedef std::string StringA;
typedef std::wstring StringW;
typedef std::vector<StringA> VecStringA;
typedef std::vector<StringW> VecStringW;

#ifdef _UNICODE
typedef StringW String;
typedef VecStringW VecString;
#else
typedef StringA String;
typedef VecStringA VecString;
#endif // _UNICODE

class StringHelper
{
public:
// 替换字符,把from替换成to
static void Replace(StringA& str, char from, char to);
static void Replace(StringW& str, wchar_t from, wchar_t to);

// 替换所有字符串,把from替换成to
static void Replace(StringA& str, const StringA& from, const StringA& to);
static void Replace(StringW& str, const StringW& from, const StringW& to);

// 转化成大写、小写
static void Toupper(StringA& str);
static void Toupper(StringW& str);
static void Tolower(StringA& str);
static void Tolower(StringW& str);

// 去除字符串两端空格
static void Trim(StringA& str);
static void Trim(StringW& str);

// 检查字符串是否以某一字串开始
static bool StartWith(const StringA& str, const StringA& head);
static bool StartWith(const StringW& str, const StringW& head);

// 检查字符串是否以某一字串结束
static bool EndWith(const StringA& str, const StringA& tail);
static bool EndWith(const StringW& str, const StringW& tail);

// 字符串转整型、长整型、浮点型
static int ToInt(const StringA& str);
static int ToInt(const StringW& str);
static long ToLong(const StringA& str);
static long ToLong(const StringW& str);
static double ToDouble(const StringA& str);
static double ToDouble(const StringW& str);

// 反转字符串
static void Reverse(StringA& str);
static void Reverse(StringW& str);

// 将from字符串反转到to
static void Reverse(const StringA& from, StringA& to);
static void Reverse(const StringW& from, StringW& to);

// 分隔字符串
static VecStringA Split(const StringA& src, const StringA& token);
static VecStringW Split(const StringW& src, const StringW& token);
};
do_fork 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lovesi3344 的回复:]
c + c++ + c# < java

引用 4 楼 do_fork 的回复:
STL > BOOST > MFC

[/Quote]

文本处理:
Java < sed+grep+awk <= python <= perl
lovesi3344 2009-12-17
  • 打赏
  • 举报
回复
c + c++ + c# < java

[Quote=引用 4 楼 do_fork 的回复:]
STL > BOOST > MFC
[/Quote]
cattycat 2009-12-17
  • 打赏
  • 举报
回复
string吧,一般都用这个,不需配置什么,如果你是MFC的工程就用CString。
boost的string可能得加boost的库。
do_fork 2009-12-17
  • 打赏
  • 举报
回复
STL > BOOST > MFC
yangxxxxxx66 2009-12-17
  • 打赏
  • 举报
回复
不要考虑这些,遇到问题再解决,我都是这样的
mstlq 2009-12-16
  • 打赏
  • 举报
回复
随便……
如果都能够满足需求的话……
哪种顺手就用哪种……
rambo_ghaip 2009-12-16
  • 打赏
  • 举报
回复
我一般用mfc,就用cstring了.
你先用最熟悉的,如果用不了,看那个能用就用那个了,不会就去查msdn。

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧