为什么 std::string 不是 std::vector 的特化? [英] Why isn't std::string a specialization of std::vector?

查看:45
本文介绍了为什么 std::string 不是 std::vector 的特化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将字符串视为字符向量似乎很明显.那么为什么 string 有它自己的特殊实现,这看起来与 vector 类的完全不同?

It seems obvious to consider a string to be a vector of characters. Why then does string have its own special implementation, which seems quite different from that of the vector class?

只是为了说明这一点,这里有一些来自两个类的片段,以表明所需的工作相当相似,例如两者都使用分配器来管理内存.具有特征也可能对向量有用.

Just to illustrate the point, here are some snippets from both classes to show that the work needed is rather similar, e.g. both using an allocator to manage memory. Also having traits could be useful for vectors as well.

从 std::string 的实现中截取的以下内容看起来似乎适合 std::vector 的更一般实现,如果我们允许向量具有类型特征.

The following snipped from the implementation of std::string looks like it would fit into the more general implementation of std::vector if we would allow a vector to have type-traits.

 139  template <class _charT, class _Traits , class _Allocator >                                        |
 140  class _RWSTDExportTemplate basic_string                                                           |
 141  {                                                                                                 |
 142  public:  
 ....
 333    size_type size () const   { return length(); }                                                  |
 334    inline size_type length () const;                                                               |
 335    size_type max_size () const                                                                     |
 336    {                                                                                               |
 337      return npos - sizeof(__rep_type)-2;                                                           |
 338    }                                                                                               |
 339    inline void resize (size_type, _charT);                                                         |
 340    void resize (size_type n)                                                                       |
 341    {                                                                                               |
 342      resize(n,__eos());                                                                            |
 343    }                                                                                               |
 344    inline size_type capacity () const;                                                             |
 345    inline void reserve (size_type=0);                                                              |
 346    void clear () { erase(); }                                                                      |
 347    bool empty () const  { return length() == 0; }   

这是来自向量:

75  template <class _Tt, class _Allocator _RWSTD_COMPLEX_DEFAULT(allocator<_Tt>) >                    |
76  class vector                                                                                      |
77  {                                                                                                 |
78       
86  public:                                                                                           |
87    //                                                                                              |
88    // Types.                                                                                       |
89    //                                                                                              |
90    typedef _Tt                                         value_type;                                 |
91    typedef _Allocator                                  allocator_type;                             |
92 
383    //                                                                                              |
384    // Capacity.
385    //
386    size_type size ()     const { return size_type(end() - begin()); }
387    size_type max_size () const { return __value_alloc_type(__end_of_storage).max_size();   }
388    void resize (size_type new_size);
389    void resize (size_type new_size, _Tt value);
390
391    size_type capacity () const { return size_type(__end_of_storage.data() - begin()); }
392    bool      empty ()    const { return begin() == end();                    }
393    void reserve (size_type n)
394    {
395      _RWSTD_THROW(n > max_size(), length_error,
396        __RWSTD::except_msg_string(__RWSTD::__rwse_InvalidSizeParam,
397          "vector::reserve(size_t)",n,max_size()).msgstr());
398
399      if (capacity() < n)
400      {
401        __value_alloc_type va(__end_of_storage);
402        iterator tmp = va.allocate(n,__start);
403#ifndef _RWSTD_NO_EXCEPTIONS
404        try {
405          uninitialized_copy(begin(), end(), tmp);
406        } catch(...) {
407          __value_alloc_type(__end_of_storage).deallocate(tmp,n);
408          throw;
409        }
410#else

推荐答案

A std::string 具有与 std::vector 不兼容的行为,特别是最后出现零值.

A std::string has behavior that is not compatible with std::vector, in particular the appearance of a zero-value at the end.

C++11 §21.4.5/2 关于 operator[],在 [string.access] 中:

C++11 §21.4.5/2 about operator[], in [string.access]:

返回:*(begin() + pos) 如果 pos ,否则是对 T 类型对象的引用,并带有值charT();引用的值不得修改.

Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type T with value charT(); the referenced value shall not be modified.

另一个很大的区别是 std::string 支持小缓冲区优化,而这种优化可能性是 不适用于 std::vector.

Another big difference is that std::string supports the small buffer optimization, while that optimization possibility is not available for std::vector.

这篇关于为什么 std::string 不是 std::vector 的特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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