They're both resizable arrays, and std::basic_string doesn't have any specifically character-related functions like upper(). What's special about string to make it better for character data?
Yes it is possible to hold two different types, you can create a vector of union types. The space used will be the larger of the types.
However, a string is not exactly the same as a vector <char> . For example, a string has push_back (you could have s. push_back(ch); to add a char to a string) but it does not have a pop_back .
From a purely philosophical point of view: yes, a string is a type of vector. It is a contiguous memory block that stores characters (a vector is a contiguous memory block that stores objects of arbitrary types). So, from this perspective, a string is a special kind of vector.
A vector will hold an object of a single type, and only a single type.
Most of the reason has to do with localization and internationalization (L10I18),performance and for historical reasons.
For the L10I18 issues, char_traits was added, and you will note that streams has these as well. The intent was to make "smarter characters" in a way, but the outcome was useless. About the only thing char_traits is good for is to specialize some of the std::string/wstring compares, copies, etc as compiler intrinsics.
The failure is mostly due to UNIX streams themselves, which see the character as the main "atom" where in GUIs, web etc that are internationalized the string is the main "atom." In other words, in C/C++ land, we have "dumb arrays of smart characters" for strings, whereas every other language uses "smart arrays of dumb characters." Unicode takes the latter approach.
Another big difference between basic_string and vector -- basic_string can only contain POD types. This can make a difference in some cases somoetime the compiler has an easier time optimizing basic_string compared to vector.
basic_string sometimes has many other optimization, such as Copy on Write and Small String Optimization. These vary from one implementation to the next.
However probably the most reason there are two things nearly the same is historical: strings predates the STL quite a bit, and most of the work seemed to center on making them interoperate with IOStream library. One C++ Urban Myth is that STL is a "container library" that was added to C++. It is not, and to get it adopted into C++, containers were added. An "STL Interface" was also bolted onto the existing string class. std::vector was largely taken from a vector implemenation that existed in the AdaSTL.
std::string has a lot of operators that std::vector doesn't:
Admittedly there is little else that std::string has that a vector doesn't or couldn't, but these are important, they are the majority of use cases for a string.
Strings do have special string-related functions: c_str
, substr
, concatenation, among others. Also, don't forget the important point that strings
automatically add the '\0'
to the end of their data (and handle it properly with concatenation etc.) so they don't have the same operation as a vector<char>
or something like that.
But yes, they are incredibly similar. They both hold a pointer to a heap-allocated array, but they are certainly not the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With