Why are there different types of strings and vectors?

They are resizable arrays, and std :: basic_string has no special character-related functions, such as upper (). What is special about a string to make it better for character data?

+5
source share
4 answers

Most of the reasons are related to localization and internationalization (L10I18), performance and historical reasons.

For L10I18 problems, char_traits has been added, and you will notice that these are also in the threads. The goal was to make “smart characters” in some way, but the result was futile. It is useful to specialize some of std :: string / wstring about a single char_traits value, compare, copy, etc. Like the built-in compiler functions.

The failure is mainly due to UNIX threads, which are seen as the main "atom", where in the GUI, networks, etc. that are internationalized, the string is the main "atom". In other words, in C / C ++, we have “dumb arrays of smart characters” for strings, while every other language uses “smart arrays of dumb characters”. Unicode uses the latter approach.

basic_string vector - basic_string POD. , , basic_string .

basic_string , Copy on Write Small String Optimization. .

, , , : STL , , , , IOStream. ++ Urban Myth - , STL " ", ​​ ++. , ++, . STL . std::vector , AdaSTL.

+10

std::string , std::vector :

  • operator + ( a b, + )
  • operator <, > , ==,!= ( , )
  • c_str() ( "C style" )
  • ( , find .., STL , )

, , std::string , , , .

+6

: c_str, substr, . , , , strings '\0' ( ..), , vector<char> - .

But yes, they are incredibly similar. They both contain a pointer to an array allocated for the heap, but they certainly do not match.

+3
source

It was a design decision at an early stage in the creation of STL. I think many people now admit that the interface is std::stringtoo bloated and incompatible with the rest of the STL, but it's too late to change it.

+1
source

All Articles