Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So what are main differences and which of them will be used in which cases?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
337 views
Welcome To Ask or Share your Answers For Others

1 Answer

  • vector<char> gives you a guarantee that &v[0]+n == &v[n] whereas a string doesn't (practically, it is the case, but there is no guarantee)... AFAIK C++0x gives that guarantee already
  • there is no implicit conversion from const char* to vector<char>
  • string is not an STL container. For example, it has no pop_back() or back() functions
  • And last, but not least, different member functions! String gives you functions suitable for strings, like returnig a null-terminated string with c_str()

Bottom line: Use string when you need to operate with strings. Use vector<char> when you need a ... well, vector of individual chars...

Another use of vector<char> is a way to avoid vector<bool> specialization.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...