What is the fundamental difference, if any, between a C++ std::vector and std::basic_string?
strings are optimized to only contain character primitives, vectors can contain primitives or objectsThe preeminent difference between vector and string is that vector can correctly contain objects, string works only on primitives. So vector provides these methods that would be useless for a string working with primitives:
Even extending string will not allow it to correctly handle objects, because it lacks a destructor. This should not be viewed as a drawback, it allows significant optimization over vector in that string can:
char_traits, one of string's template arguments, to define how operations should be implemented on the contained primitives (of which only char, wchar_t, char16_t, and char32_t are implemented: http://en.cppreference.com/w/cpp/string/char_traits)Particularly relevant are char_traits::copy, char_traits::move, and char_traits::assign obviously implying that direct assignment, rather than construction or destruction will be used which is again, preferable for primitives. All this specialization has the additional drawbacks to string that:
char, wchar_t, char16_t, or char32_t primitives types will be used. Obviously, primitives of sizes up to 32-bit, could use their equivalently sized char_type: https://stackoverflow.com/a/35555016/2642059, but for primitives such as long long a new specialization of char_traits would need to be written, and the idea of specializing char_traits::eof and char_traits::not_eof instead of just using vector doesn't seem like the best use of time.vector iterator, but string iterators are additionally invalidated by string::swap and string::operator=Additional differences in the interfaces of vector and string:
string::data: Why Doesn't std::string.data() provide a mutable char*?string provides functionality for working with words unavailable in vector: string::c_str, string::length, string::append, string::operator+=, string::compare, string::replace, string::substr, string::copy, string::find, string::rfind, string::find_first_of, string::find_first_not_of, string::flind_last_of, string::find_last_not_of, string::operator+, string::operator>>, string::operator<<, string::stoi, string::stol, string::stoll, string::stoul, string::stoull, string::stof, string::stod, string::stold, stirng::to_string, string::to_wstringvector accepts arguments of another vector, string accepts a string or a char*Note this answer is written against C++11, so strings are required to be allocated contiguously.