std :: string vs. char * [英] std::string vs. char*
本文介绍了std :: string vs. char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
std :: string在堆栈或堆上存储的数据与char *不同吗?还是只是从char *派生到一个类中?
does std::string store data differently than a char* on either stack or heap or is it just derived from char* into a class?
推荐答案
char *
- 是您的CPU体系结构的一个指针的大小.
- 可能是从
malloc
或calloc
或new
或new []
返回的值.- 如果是这样,完成后必须传递给
free
或delete
或delete []
. - 如果是这样,字符将存储在堆中.
- Is the size of one pointer for your CPU architecture.
- May be a value returned from
malloc
orcalloc
ornew
ornew[]
.- If so, must be passed to
free
ordelete
ordelete[]
when you're done. - If so, the characters are stored on the heap.
- 通常,无法判断
char *
参数是否指向堆栈,堆或全局空间. - Generically, no way to tell if a
char*
argument points to stack, heap, or global space. - 是几个指针的大小,通常是三个.
- 在创建时自行构造:不需要
new
或delete
.- 拥有字符串的副本(如果字符串可能会更改).
- 可以从
char *
复制此字符串. - 默认情况下,内部使用
new []
的方式与获取char *
的方式相同.
- Is the size of several pointers, often three.
- Constructs itself when created: no need for
new
ordelete
.- Owns a copy of the string, if the string may be altered.
- Can copy this string from a
char*
. - By default, internally uses
new[]
much as you would to obtain achar*
.
- 定义
c_str()
,该代码将返回char *
以供临时使用.
- Defines
c_str()
which returns achar*
for temporary use.
-
string :: iterator
是灵活的:一种实现可能使它成为范围检查的超级安全调试助手,或者只是使之成为超高效的char *
一个开关.
string::iterator
is flexible: an implementation may make it a range-checked super-safe debugging helper or simply a super-efficientchar*
at the flip of a switch.
这篇关于std :: string vs. char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- If so, must be passed to
char*
- 如果是这样,完成后必须传递给
查看全文