std :: string和std :: vector< char&gt ;?之间有什么区别? [英] What are differences between std::string and std::vector<char>?

查看:324
本文介绍了std :: string和std :: vector< char&gt ;?之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,什么是主要差异,哪些是在哪些情况下使用的?

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

推荐答案


  • 向量< char> 可以保证& v [0] + n ==& v [n] 但是字符串不会(实际上是这种情况,但不能保证)... AFAIK C ++ 0x已经提供了保证

  • 没有隐式转换从 const char * 向量< char>

  • 字符串不是STL容器。例如,它没有 pop_back() back()函数

  • 最后,但并非最不重要的,不同的成员函数! String给出了适用于字符串的函数,例如returnig以 c_str()

    • 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()
    • 底线: 当您需要使用字符串操作时,使用 string 。使用向量< char> 当你需要一个...好,单个字符的向量...

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

      使用向量< char> 是一种避免向量< bool> 专业化的方法。

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

      这篇关于std :: string和std :: vector&lt; char&gt ;?之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆