I wrote my own string class because std::string is a steaming load. Though if I was forced to choose between std::string and char arrays I'd use std::string.
as for vectors/arrays -- it depends on the situation, but I lean towards vectors except in situations which have extreme performance concerns (which isn't often!)
C string (through I've read I probably shouldn't use either or at most not the C string) and I guess vectors. They're a lot safer and much easier. I'll use arrays if the array never needs to change and performance might matter.
string, vector<char>, char[] in that order. String provides a safe and standard means of manipulating text in C++. A vector<char> provides the benefits of C++ containers. And a char[] can be fast where performance is a higher priority than code safety (or where you can guarantee the code is correct).
vector before array for the same reasons stated above.
Hi Ivan!
The answer is simple:
If the length of the array never has to change, then use normal arrays and for text use, of course, normal arrays of chars which should be terminated with a NULL character.
If the number of elements (length) of your array needs to change, then use dynamic arrays instead of normal arrays and, for text, use string instead of NULL terminated arrays of chars.
Remember!!!
Vectors aren't the only solution to normal arrays.They are the most used because they fit the purpose most of the time and they are from the Standard Library.But the Standard Template Library also offers other dynamic arrays or containers such as queue, deque, etc.Those are better in some cases.
That's what I used to think, but I read the reference on deques yesterday, and actually they are better suited than vectors for most situations.
Think about it. When do you need a vector? When you need to store an unknown number of elements in no particular order so they can later be randomly and quickly retrieved. Both vectors and deques provide this, but deques are also optimized for pushing and popping at either end, which is the most common way of filling a vector.
However, if you were also to require contiguity between the elements, then a vector is the way to go.
vector has faster access because all elements are contiguous (ie: it doesn't have to check to see which segment the desired element is in prior to lookup -- it can just do a straight index).
So while deque certainly isn't slow... if you're doing a lot of random access, then vector will be faster and more preferable unless you'll be pushing/popping in front.
...and unless you have huge amounts of data to store in the deque. (Vectors are limited in how much they can store simply because they require continuous memory.)