Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stl - Is a string a vector?

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?

like image 729
Yakov Avatar asked Nov 01 '13 16:11

Yakov


3 Answers

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.

like image 119
Zac Howland Avatar answered Oct 23 '22 00:10

Zac Howland


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.

like image 37
BoBTFish Avatar answered Oct 23 '22 02:10

BoBTFish


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.

like image 33
Yu Hao Avatar answered Oct 23 '22 00:10

Yu Hao