哪些字符串类在C ++中使用? [英] Which string classes to use in C++?

查看:157
本文介绍了哪些字符串类在C ++中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C ++(MFC)中有一个多线程桌面应用程序。目前开发人员使用CString或std :: string,可能取决于他们的心情。所以我们想选择一个单独的实现(可能是除了那两个之外的其他实现)。

we have a multi-threaded desktop application in C++ (MFC). Currently developers use either CString or std::string, probably depending on their mood. So we'd like to choose a single implementation (probably something other than those two).

MFC的CString是基于写时复制(COW)有些人声称这在多线程环境中是不可接受的(并且可能参考了本文)。我不相信这样的说法,因为原子计数器似乎是相当快,而且这种开销不知何故得到减少内存重新分配补偿。

MFC's CString is based on copy-on-write (COW) idiom, and some people would claim this is unacceptable in a multithreaded environment (and probably reference to this article). I am not convinced by such claims, as atomic counters seem to be quite fast, and also this overhead is somehow compensated by a reduction in memory re-allocations.

我学到了std :: string实现依赖于编译器 - 它不是COW在MSVC,但它是,或者是在gcc。据我所知,新的C ++ 0x标准将通过要求非COW实现并解决一些其他问题,如连续的缓冲区要求来解决这个问题。所以实际上std :: string看起来不太好定义在这一点...

I learned that std::string implementation depends on compiler - it is not COW in MSVC but it is, or was in gcc. As far as I understood, the new C++0x standard is going to fix this by requiring a non-COW implementation and resolve some other issues, such as contiguous buffer requirements. So actually std::string looks not well defined at this point...

一个快速的例子,我不喜欢std :: string:没有办法从一个函数返回一个字符串,没有过多的重新分配(复制构造函数,如果返回值,没有访问内部缓冲区优化,所以返回参考例如 std :: string& Result 没有帮助)。我可以这样做与CString通过返回值(没有复制由于COW)或通过引用传递和直接访问缓冲区。再次,C ++ 0x救援与其右值引用,但我们不会有C ++ 0x在最近的功能。

A quick example of what I don't like about std::string: no way to return a string from a function without excessive re-allocations (copy constructor if return by value, and no access to internal buffer to optimize that so "return by reference" e.g. std::string& Result doesn't help). I can do this with CString by either returning by value (no copy due to COW) or passing by reference and accessing the buffer directly. Again, C++0x to the rescue with its rvalue references, but we are not going to have C++0x in the nearest feature.

哪个字符串类应该使用? COW真的会成为一个问题吗?有没有其他常用的有效的字符串实现?感谢。

Which string class should we use? Can COW really become an issue? Are there other commonly used efficient implementations of strings? Thanks.

编辑:我们目前不使用unicode,我们不太可能需要它。

We don't use unicode at the moment, and it is unlikely that we will need it. However, if there is something easily supporting unicode (not at the cost of ICU...), that would be a plus.

推荐答案



  • 更好地与现有的C ++库进行交互

-问题。编译器非常擅长执行返回值优化(RVO),在大多数情况下,通过值返回时,实际上会消除副本。

The "return by value" issue is mostly a non-issue. Compilers are very good at performing Return Value Optimization (RVO) which actually eliminates the copy in most cases when returning by value. If it doesn't, you can usually tweak the function.

COW 因为某个原因而被拒绝:它不会缩放),并且希望的增加的速度没有真正被测量(见Herb Sutter的文章)。原子操作不像他们出现的那么便宜。使用单处理器单核很容易,但现在多核是商品和多处理器广泛可用(对于服务器)。在这种分布式架构中,有多个高速缓存,需要同步,分布式架构越多,原子操作的成本越高。

COW has been rejected for a reason: it doesn't scale (well) and the so-hoped-for increase in speed has not been really measured (see Herb Sutter's article). Atomic operations are not as cheap as they appear. With mono-processor mono-core it was easy, but now multi-core are commodity and multi-processors are widely available (for servers). In such distributed architectures there are multiple caches, that need be synchronized, and the more distributed the architecture, the more costly the atomic operations.

CString 实施小字符串优化?这是一个简单的技巧,它允许字符串不为小字符串(通常是几个字符)分配任何内存。非常有用,因为事实证明大多数字符串事实上很小,你的应用程序中有多少字符串少于8个字符长?

Does CString implement Small String Optimization ? It's a simple trick that allows a string not to allocate any memory for small strings (usually a few characters). Very useful because it turns out that most strings are in fact small, how many strings in your application are less than 8-characters long ?

因此,除非你介绍我真实的基准,清楚地表明使用 CString 的净收益,我更喜欢坚持标准:它是标准的,可能更好的优化。

So, unless you present me a real benchmark which clearly shows a net gain in using CString, I'd prefer sticking with the standard: it's standard, and likely better optimized.

这篇关于哪些字符串类在C ++中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆