Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ strings vs vector<char> [duplicate]

Tags:

c++

string

vector

It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.

However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?

like image 398
DevInd Avatar asked Mar 14 '23 11:03

DevInd


1 Answers

I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.

To be clear: it is all about expressing the intent.

like image 81
cdonat Avatar answered Mar 23 '23 08:03

cdonat