为什么是字符串和向量不同的类型? [英] Why are string and vector distinct types?

查看:429
本文介绍了为什么是字符串和向量不同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们都是可调整大小的数组,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?

推荐答案

大多数原因与本地化和国际化有关),性能和历史原因。

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

对于L10I18问题,添加了char_traits,你会注意到流也有这些。目的是使智慧人物在某种程度上,但结果是没有用的。关于char_traits唯一的好处是专门将某些std :: string / wstring比较,副本等作为编译器内在函数。

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.

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

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.

basic_string和vector - basic_string之间的另一个大的区别只能包含POD类型。这在某些情况下可能会有所不同,在某些情况下,编译器比向量更容易优化basic_string。

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有时还有许多其他优化,例如Copy on Write和Small字符串优化。

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

但是可能最大的原因是有两个事情几乎是相同的历史:字符串先于STL有点,大多数工作似乎集中在使它们与IOStream库的互操作。一个C ++ Urban Myth是STL是添加到C ++的容器库。它不是,并得到它采用到C + +,容器被添加。 STL接口也被固定在现有的字符串类上。 std :: vector主要取自存在于AdaSTL中的载体实施。

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.

这篇关于为什么是字符串和向量不同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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