为什么字符串和向量是不同的类型?

发布于 2024-08-25 08:08:15 字数 92 浏览 1 评论 0原文

它们都是可调整大小的数组,并且 std::basic_string 没有任何与字符相关的特定函数,例如 upper()。字符串有什么特别之处,可以让它更好地处理字符数据?

They're both resizable arrays, and std::basic_string doesn't have any specifically character-related functions like upper(). What's special about string to make it better for character data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤者何惧 2024-09-01 08:08:15

大部分原因与本地化和国际化(L10I18)、性能和历史原因有关。

对于 L10I18 问题,添加了 char_traits,您会注意到流也有这些。其目的是在某种程度上创造“更聪明的角色”,但结果毫无用处。 char_traits 唯一有用的就是将一些 std::string/wstring 比较、复制等专门化为编译器内在函数。

失败主要是由于 UNIX 流本身,它将字符视为主要“原子”,而在国际化的 GUI、Web 等中,字符串是主要“原子”。换句话说,在 C/C++ 领域,我们为字符串提供“智能字符的哑数组”,而其他所有语言都使用“哑字符的智能数组”。 Unicode 采用后一种方法。

basic_string 和 vector 之间的另一个大区别是 basic_string 只能包含 POD 类型。在某些情况下,与向量相比,编译器可以更轻松地优化 basic_string ,这可能会产生影响。

basic_string有时还有许多其他优化,例如Copy on Write和Small String Optimization。这些因不同的实现而异。

然而,有两件事几乎相同的最主要原因可能是历史性的:字符串早于 STL 相当早,并且大部分工作似乎集中在使它们与 IOStream 库进行互操作。一个 C++ 都市神话是 STL 是一个添加到 C++ 中的“容器库”。事实并非如此,为了将其采用到 C++ 中,添加了容器。 “STL 接口”也被固定到现有的字符串类上。 std::vector 很大程度上取自 AdaSTL 中存在的向量实现。

Most of the reason has to do with localization and internationalization (L10I18),performance and for historical reasons.

For the L10I18 issues, char_traits was added, and you will note that streams has these as well. The intent was to make "smarter characters" in a way, but the outcome was useless. About the only thing char_traits is good for is to specialize some of the std::string/wstring compares, copies, etc as compiler intrinsics.

The failure is mostly due to UNIX streams themselves, which see the character as the main "atom" where in GUIs, web etc that are internationalized the string is the main "atom." In other words, in C/C++ land, we have "dumb arrays of smart characters" for strings, whereas every other language uses "smart arrays of dumb characters." Unicode takes the latter approach.

Another big difference between basic_string and vector -- basic_string can only contain POD types. This can make a difference in some cases somoetime the compiler has an easier time optimizing basic_string compared to vector.

basic_string sometimes has many other optimization, such as Copy on Write and Small String Optimization. These vary from one implementation to the next.

However probably the most reason there are two things nearly the same is historical: strings predates the STL quite a bit, and most of the work seemed to center on making them interoperate with IOStream library. One C++ Urban Myth is that STL is a "container library" that was added to C++. It is not, and to get it adopted into C++, containers were added. An "STL Interface" was also bolted onto the existing string class. std::vector was largely taken from a vector implemenation that existed in the AdaSTL.

七色彩虹 2024-09-01 08:08:15

std::string 有很多 std::vector 没有的运算符:

  • 运算符 + (将字符串 a 附加到字符串 b,+ 对向量没有真正意义)
  • 运算符 <、>、==、!= (字符串比较,有些对向量没有意义)
  • c_str() (返回“C 风格”表示)
  • 以及更多(包括子字符串、查找等,但其中一些在 STL 中的其他地方实现,可以在向量上使用,有点)

诚然,std::string 几乎没有什么是向量没有或不能的,但这些很重要,它们是字符串的大多数用例。

std::string has a lot of operators that std::vector doesn't:

  • operator + (append string a to string b, + doesn't really make sense for vectors)
  • operator <, >, ==, != (string comparison, some don't make sense for vectors)
  • c_str() (return a "C style" representation)
  • And more (including substring, find, etc but some of these are implmeneted elsewhere in the STL and can be use on vectors, sort of)

Admittedly there is little else that std::string has that a vector doesn't or couldn't, but these are important, they are the majority of use cases for a string.

彡翼 2024-09-01 08:08:15

字符串确实具有与字符串相关的特殊函数:c_strsubstr、连接等。另外,不要忘记重要的一点,字符串会自动将'\0'添加到其数据的末尾(并通过连接等正确处理它),以便它们没有与 vector 或类似的操作相同的操作。

但是,是的,它们非常相似。它们都持有一个指向堆分配数组的指针,但它们肯定不一样。

Strings do have special string-related functions: c_str, substr, concatenation, among others. Also, don't forget the important point that strings automatically add the '\0' to the end of their data (and handle it properly with concatenation etc.) so they don't have the same operation as a vector<char> or something like that.

But yes, they are incredibly similar. They both hold a pointer to a heap-allocated array, but they are certainly not the same.

随遇而安 2024-09-01 08:08:15

这是 STL 创建初期的设计决策。我想现在很多人都承认std::string的接口过于臃肿并且与STL的其余部分不一致,但是现在改变它已经太晚了。

It was a design decision early in the STL creation. I think a lot of people now admit that std::string's interface is too bloated and inconsistent with the rest of the STL, but it's too late to change it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文