Why is a string not a (subclass) of a vector?

Lines

C are char arrays.

Vectors are new arrays in C ++.

Why now there are no line vectors (characters)?

Most vector and string methods are also duplicated. Is there a reason to make strings in C ++ different?

+4
source share
3 answers

It is pretty much just historical. Strings and vectors were developed in parallel with a little thought about how they can be considered the same for T==char.

This also explains why standard containers are nice and versatile, while it std::basic_stringis a complete monolith of a member function after a member function.

, std::basic_string<T, Alloc> std::vector<T, Alloc> . , . , , GCC , .

std::string::end() ( '\0' ) . .c_str() std::vector<char> .

tl; dr: , ,

+2

Vector vs string vector string, - , , .

, , , , -, , .

, , string, , vector, , , , -, , , , vector.

+2

Yes, since the vector is a container that can contain T and std :: string, is a wrapper around char * and cannot contain int or other data types. It would not make sense to have it in any other way.

0
source

All Articles