c++ - 为什么 std::string 不是 std::vector 的特化?

标签 c++ string vector stl

<分区>

将字符串视为字符 vector 似乎很明显。那为什么string有自己的特殊实现,看起来和vector类的实现很不一样呢?

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

从 std::string 的实现中截取的以下内容看起来适合更通用的 std::vector 实现,如果我们允许 vector 具有类型特征的话。

 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; }   

这是来自 vector :

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

最佳答案

以这个片段为例:

string s = "abc";

没有容器具有类似的初始化语法,它接收指向第一个元素的指针并扫描序列以寻找特殊的终止符元素。使用 std::vector至少会很麻烦。由于文本在计算中很常见,因此拥有方便的文本容器类型是必需的,并且 std::vector就是不合适。

也就是说,我可以想象std::string继承std::vector<char>私下或聚合它,但这与它的特化完全不同。它也不能是特化,因为如果你想要 vector<char> 怎么办?那不像一个字符串?然后,您将拥有与 vector<bool> 相同的 fubar。今天。

关于c++ - 为什么 std::string 不是 std::vector 的特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418145/

相关文章:

c++ - 错误的计数器路径,pdhAddCounter; Windows 中的性能监视器

c++ - 直接显示捕获过滤器 "wrapper"

python - 使用 python 正则表达式删除除组合为字符串的数字之外的所有数字

C++ 纸牌使用 vector<Card*> 错误与 vector.erase 函数

c++ - 固定大小的 std::span 与 std::array

c++ - 将 "normal"std::string 转换为 utf-8

c++ - 我可以从 nullptr 构造一个 string_view 吗?

C# 字符串格式标志或修饰符到小写参数

c++11 - 用较小的 std::vector 替换 std::vector 的一部分

c++ - 为什么这个程序有这种行为(push_back)?