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

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

问题描述

那么主要区别是什么?在哪些情况下将使用哪些区别?

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

推荐答案


  • vector< char> 给您保证& v [0] + n ==& v [n] 而不是字符串(实际上是这样,但是没有保证)... AFAIK C ++ 0x已经提供了保证

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

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

  • 最后,但同样重要的是,不同的成员函数!字符串为您提供适合于字符串的函数,例如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 。当需要...时,使用 vector< char> ...好吧,单个字符的向量...

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

      另一个使用 vector< char> 是避免 vector< bool> 专业化的一种方法。

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

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

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