I have encountered a question in some quiz
"Is a string a vector? If yes, in what way? If no, why not?
"
Both of them have random access to the content.
But string has some methods which vector dosn`t.It also might have reference count .
So it is obvious that string is not exactly a vector (typedef string vector)
Are there known implementations in which class string : public vector <char>
?
If not - what is the reason for not implementing it so?
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.
In terms of design and implementation of std::string
and std::vector
, they share some of the same interface elements (e.g. contiguous memory blocks, operator[]
), but std::string
does not derive from std::vector
(side note: you should not publicly derive from standard containers as they are not designed to be based classes - e.g. they do not have virtual destructors), nor are they directly convertible to each other. That is, the following will not compile:
std::string s = "abc";
std::vector<char> v = s; // ERROR!
However, since they both have iterator support, you can convert a string to a vector:
std::string s = "abc";
std::vector<char> v(s.begin(), s.end()); // note that the vector will NOT include the '\0' character
std::string
will no longer have a reference count (as of C++11) as the copy-on-write functionality that many implementations used was forbidden by the C++11 standard.
From a memory perspective, an instance of std::string
will look very similar to a std::vector<char>
(e.g. they both will have a pointer to their memory location, a size, a capacity), but the functionality of the two classes is different.
std::string
has a non-trivial part of it's interface in common with std::vector
(and other standard containers), but it is very definitely a different thing, with a different purpose.
It may also be implemented very differently, as it allows for things like the small string optimisation, or copy-on-write (not legal since 2011). (Although it is certainly possible for them to have very similar implementations).
They both support random access iterators, so can be used in similar ways with standard algorithms. I think std::string
can not be classified as a sequence container.
It would not be possible to implement many of std::string
's member functions directly by inheriting from std::vector
, because it hides the fact that it is also storing a NUL
-terminator. So when std::string::size
returns 3
, std::vector::size
would return 4
, for instance. Same goes for end
, and a few others.
No, std::string
(std::basic_string<char>
), you can think of it a type of sequence container that contains char
as it shares many functions with other containers, but it's not implemented using std::vector
.
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