5

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 ?

DevInd
  • 1,645
  • 2
  • 11
  • 17

2 Answers2

7

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.

cdonat
  • 2,748
  • 16
  • 24
  • To be clear: it is about expressing intent. – cdonat Oct 14 '15 at 18:12
  • 1
    Good comment. Belongs in the answer. I recommend an edit to the answer to beat that point home. – user4581301 Oct 14 '15 at 18:14
  • You can do the same things with string too coz string too has push_back, pop_back, etc type functions which vector uses – DevInd Oct 14 '15 at 18:14
  • 1
    @cdonat, i guess the doubt raised by CppNITR is valid, provide a better justification atleast – Anwesha Oct 14 '15 at 18:20
  • 1
    @CppNITR Yes, but this is a readability issue as much as anything else. A reader of your code sees `string`, they are going to expect certain things to be true, not the least of which is the string variable contains readable, and probably printable, text. A `vector` contains. What it contains varies based on the template argument and the interpretation of the content is wider in scope. – user4581301 Oct 14 '15 at 18:29
  • Sure a string can be used for that as well. Basically everything could ever be done with std::string as well as with std::vector. I have edited my answer, as suggested by @user4581301, to make clear, that it is about the intent. Is the data used as something text-like, or as a collection of otherwise not connected char-objects? – cdonat Oct 14 '15 at 18:29
  • yeah but for byte more natural for me is unsigned char e.g. range 0-255 –  Oct 14 '15 at 18:58
  • @quser In my example I do store independent character values in the vector, not just bytes. Since I said, it is about expressing the intent, we here have an ordered collection of otherwise unrelated characters. It is not anything text-like, and therefore no `std::string`, and it is actually characters, not just bytes, and therefore no `std::vector` or the like. – cdonat Oct 14 '15 at 19:05
1

To put it briefly: if you're storing text, then string, otherwise vector<char>.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • 1
    Would you mind elaborating with a good example – DevInd Oct 14 '15 at 18:02
  • if it is a byte array would not you use unsigned char instead of char? –  Oct 14 '15 at 18:15
  • 1
    @quser In that case I would prefer a `vector` with `using byte = char;` to make it clear it's storing raw binary data and not characters. See [this answer](http://stackoverflow.com/a/8386615/3425536) why `char` instead of `unsigned char`. – Emil Laine Oct 14 '15 at 18:18
  • Also the C++ standard library itself uses `char`, not `unsigned char`, for binary I/O, e.g. [`ostream::write`](http://www.cplusplus.com/reference/ostream/ostream/write/). So you avoid a lot of nasty casts with `using byte = char`. – Emil Laine Oct 14 '15 at 18:25
  • @zenith: you mean there is issue if I used unsigned char instead? –  Oct 14 '15 at 18:42
  • @quser Yes, the issue that you need to do the casting when doing binary I/O with the C++ standard library. – Emil Laine Oct 14 '15 at 18:50
  • @zenith: Why? does C++ I/O require char pointers as arguments? honestly I didn't really get the answer you linked to –  Oct 14 '15 at 18:57
  • Yes. See the documentation on e.g. the `istream`/`ostream` binary read/write functions. They accept char pointers. – Emil Laine Oct 14 '15 at 19:05