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

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c++ - What are differences between std::string and std::vector<char>?

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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...