Why not std :: string specialization std :: vector?

It seems obvious that the string is a character vector. Why does the string have its own special implementation, which seems completely different than the vector class?

To illustrate this point, here are some snippets from both classes to show that the required work is pretty similar, for example. both use a allocator to manage memory. Also, the presence of signs can be useful for vectors.

The following is outlined below: std :: string looks like it will fit into the more general implementation of std :: vector if we allow the vector to have type attributes.

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

And this is from the 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 
+7
c ++ string vector stl
source share
2 answers

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

C ++ 11 ยง21.4.5 / 2 near operator[] , in [string.access]:

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

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

+2
source share

Take for example this snippet:

 string s = "abc"; 

There is no container with the same initialization syntax, where it gets a pointer to the first element and looks at the sequence for the special terminator element. Using std::vector would be cumbersome, at least. Since texts are common for calculations, the presence of a convenient text container is simply necessary, and std::vector simply not suitable.

However, I could imagine std::string inheriting std::vector<char> privately or aggregating it, but this is a completely different issue than its specialization. This also cannot be a specialization, because if you want the vector<char> not to behave like a string? Then you will have the same fubar as with vector<bool> .

+3
source share

All Articles