Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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

like image 735
Mihran Hovsepyan Avatar asked Jul 01 '11 12:07

Mihran Hovsepyan


1 Answers

  • 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.

like image 103
Armen Tsirunyan Avatar answered Oct 02 '22 01:10

Armen Tsirunyan