[英]std::string vs. char*
std :: string存储数据的方式与堆栈或堆上的char *不同,还是只是从char *派生到类中?
char*
malloc
or calloc
or new
or new[]
. 可以是从malloc
或calloc
或new
或new[]
返回的值。
free
or delete
or delete[]
when you're done. 如果是这样,必须在完成后传递给free
或delete
或delete[]
。 char[ N ]
(constant N) array or string literal. 可能是由char[ N ]
(常数N)数组或字符串文字的“分解”引起的。
char*
argument points to stack, heap, or global space. 通常,无法判断char*
参数是否指向堆栈,堆或全局空间。 <algorithm>
and such. 然而,实现RandomAccessIterator接口以与<algorithm>
等一起使用。 std::string
new
or delete
. 创建时构造自身:不需要new
或delete
。
char*
. 可以从char*
复制此字符串。 new[]
much as you would to obtain a char*
. 默认情况下,内部使用new[]
就像获取char*
。 char*
or literal. 提供隐式转换,使char*
或literal透明化。 c_str()
which returns a char*
for temporary use. 定义c_str()
,它返回一个char*
供临时使用。 std::string::iterator
type with begin()
and end()
. 使用begin()
和end()
实现std::string::iterator
类型。
string::iterator
is flexible: an implementation may make it a range-checked super-safe debugging helper or simply a super-efficient char*
at the flip of a switch. string::iterator
非常灵活:一个实现可以使它成为一个范围检查的超级安全调试助手,或者只是一个交换机翻转时的超高效char*
。 If you mean, does it store contiguously, then the answer is that it's not required but all known (to me, anyway) implementations do so. 如果你的意思是,它是否连续存储,那么答案是它不是必需的,但所有已知的(对我而言)实现都这样做。 This is most likely to support the c_str()
and data()
member requirements, which is to return a contiguous string (null-terminated in the case of c_str()
) 这很可能支持c_str()
和data()
成员要求,即返回连续的字符串(在c_str()
的情况下以null结尾)
As far as where the memory is stored, it's usually on the heap. 就存储器的存储位置而言,它通常在堆上。 But some implementations employ the "Short String Optimization", whereby short string contents are stored within a small internal buffer. 但是一些实现采用“短字符串优化”,其中短字符串内容存储在小的内部缓冲区中。 So, in the case that the string object is on the stack, it's possible that the stored contents are also on the stack. 因此,在字符串对象在堆栈上的情况下,存储的内容也可能在堆栈上。 But this should make no difference to how you use it, since one the object is destroyed, the memory storing the string data is invalidated in either case. 但这对你如何使用它没有任何影响,因为一个对象被破坏,存储字符串数据的内存在任何一种情况下都是无效的。
(btw, here's an article on a similar technique applied generally , which explains the optimization.) (顺便说一下,这里有一篇关于类似技术的文章 ,它解释了优化。)
These solve different problems. 这解决了不同的问题。 char*
(or char const*
) points to a C style string which isn't necessarily owned by the one storing the char*
pointer. char*
(或char const*
)指向C样式字符串,该字符串不一定由存储char*
指针的字符串所拥有。 In C, because of the lack of a string type, necessarily you often use char*
as "the string type". 在C中,由于缺少字符串类型,您必须经常使用char*
作为“字符串类型”。
std::string
owns the string data it points to. std::string
拥有它指向的字符串数据。 So if you need to store a string somewhere in your class, chances are good you want to use std::string
or your librarie's string class instead of char*
. 因此,如果您需要在类中的某个位置存储字符串,那么您可能希望使用std::string
或librarie的字符串类而不是char*
。
On contiguity of the storage of std::string
, other people already answered. 关于std::string
存储的连续性,其他人已经回答了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.