cstring string 比较之二(学习笔记)

C++字符串类型选择:std::string vs CString
本文对比了C++中的std::string和CString的区别,包括跨平台性、接口、性能等方面,帮助开发者根据项目需求选择合适的字符串类型。

cstring微软提供的,好象是ATL类库中提供的吧,只有vc带的库里面有。

cstring耦合了根多的微软的东西,不能跨平台,只能在windows上使用。

而string一般是指标准库中提供的std::string这个东西只要是标准的C++编译器里面就应该有这个东西的。

string尽管各家编译器实现不一样,但是接口什么的东西是相同的,意味着可以跨平台。

==各种滋味,自己品味有使用 string的各种感受

http://topic.youkuaiyun.com/u/20100309/10/6fb6a8d2-b4e2-42a9-a2c0-4914adac16cf.html

 

 

如果使用WTL开发,那就肯定不会考虑跨平台(也跨不了,连编译器都跨不了)。

所以,我认为CString是最好的选择。

 

我现在使用wxWidgets,我就会用wxString。

而当我用Qt开发时,我会用QString。

 

已经尽量在避免使用std::string了,因为要跨编译器。

GCC的std::string的引用计数,让我总提心吊胆。

即使你使用std::string,在可移植性上也要小心。

 

因为VC7以上的std::string不使用引用计数,而GCC的std::string是使用引用计数的。

 

引用计数的实现有线程安全问题。

==

==============

Reply 1 >> Re: Cstring Vs Std:String

 

 

No.

They are different...

It's not only portability, it's the public interface, too.

For example:

1) CString plays well with Windows TCHAR model

(instead you should overcome this with STL strings, using something like

typedef std::basic_string< TCHAR > tstring; or something similar...).

2) CString offers useful methods like Left, Right, etc. to extract subparts

of the string.

Moreover, it offers methods to trim trailing spaces, etc.

3) CString is reference counted, instead I believe that std::string is not.

//有对象概念

 

4) CString has an implicit operator LPCTSTR such that you can use a Cstring

 

whenever an API or something requires an LPCTSTR (const TCHAR *), instead

you must call explicitly call .c_str() method from std::string.

Probably there are other differences also...

If you want to write Windows-only code, you may want to use CString, IMHO

(but I believe that some valuable programmers here would suggest std::string

instead...).

转载于:https://www.cnblogs.com/titer1/archive/2011/12/31/2308808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值