问题描述
我们每个人都知道我们应该更喜欢 C++
中的 string
类用于所有字符串应用程序,因为它们执行许多特殊功能 &他们的成长能力动态减少.string
用于characters
,vector
用于其他数据类型 &类,因为它显示了出色的性能.
It's known to everyone of us that we should prefer string
class in C++
for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string
is for characters
, vector
is for other data types & classes because it shows great performance.
但是,在任何情况下,我们需要更喜欢 vector
(我很少看到)而不是 string
吗?
However is there any situation where we would need to prefer vector<char>
(which I see seldom) over string
?
推荐答案
只有当我明确打算存储一个不是字符串的 char 值数组时,我才会使用 vector
.例如.如果出于某种原因我要收集特定文本中某处使用的所有字符,结果可能是 vector
.
I'd use vector<char>
only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>
.
要明确:一切都是为了表达意图.
To be clear: it is all about expressing the intent.
这篇关于C++ 字符串与向量<char>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!