简体   繁体   English

std :: string vs. char *

[英]std::string vs. char*

std :: string存储数据的方式与堆栈或堆上的char *不同,还是只是从char *派生到类中?

char*

  • Is the size of one pointer for your CPU architecture. 是您的CPU架构的一个指针的大小。
  • May be a value returned from malloc or calloc or new or new[] . 可以是从malloccallocnewnew[]返回的值。
    • If so, must be passed to free or delete or delete[] when you're done. 如果是这样,必须在完成后传递给freedeletedelete[]
    • If so, the characters are stored on the heap. 如果是这样,则字符存储在堆上。
  • May result from "decomposition" of a char[ N ] (constant N) array or string literal. 可能是由char[ N ] (常数N)数组或字符串文字的“分解”引起的。
    • Generically, no way to tell if a char* argument points to stack, heap, or global space. 通常,无法判断char*参数是否指向堆栈,堆或全局空间。
  • Is not a class type. 不是班级类型。 It participates in expressions but has no member functions. 它参与表达式但没有成员函数。
  • Nevertheless implements the RandomAccessIterator interface for use with <algorithm> and such. 然而,实现RandomAccessIterator接口以与<algorithm>等一起使用。

std::string

  • Is the size of several pointers, often three. 几个指针的大小,通常是三个。
  • Constructs itself when created: no need for new or delete . 创建时构造自身:不需要newdelete
    • Owns a copy of the string, if the string may be altered. 如果字符串可能被更改,则拥有该字符串的副本。
    • Can copy this string from a char* . 可以从char*复制此字符串。
    • By default, internally uses new[] much as you would to obtain a char* . 默认情况下,内部使用new[]就像获取char*
  • Provides for implicit conversion which makes transparent the construction from a char* or literal. 提供隐式转换,使char*或literal透明化。
  • Is a class type. 是一种类型。 Defines other operators for expressions such as catenation. 为表达式定义其他运算符,例如catenation。
    • Defines c_str() which returns a char* for temporary use. 定义c_str() ,它返回一个char*供临时使用。
  • Implements 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM