如何确定字符串大小? [英] how is string size determined?

查看:129
本文介绍了如何确定字符串大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串是一个字符数组吗?

所有(静态)数组都必须在执行前设置它的大小

这会发生在哪里?

另外,当我创建一个字符串数组并将其指针增加一个以转到下一个字符串时系统如何知道下一个地址?



另外,为什么我们不能拥有不同数据类型的数组呢?是因为内存大小?

如果我创建一个包含64位整数的结构A和一个包含两个32的结构B -bit整数为什么我不能把它们放在同一个数组中?



非常感谢你!

解决方案

字符串的长度在运行时动态确定。 String有长度和容量属性,当你定义一个新字符串时,它会分配额外的空间来创建额外的缓冲区,以防你扩展这个字符串。



例如,



 string str(  abcdefgh); 





长度:8

容量:15



如果容量为8,则在字符串中添加任何字符都会强制重新分配。通过使容量大于实际字符串,这为用户提供了一些缓冲空间来扩展字符串,然后才需要重新分配。重新分配是昂贵的,因为字符串中的每个字符都必须复制到新的内存。



当你创建一个字符串数组时,你正在创建一个数组包含字符串起始地址的指针。



如果你想创建不同类型的数组,你需要使用像这样的void指针数组;



  void  * ary [ 10 ]; 
ary [ 0 ] = new int < /跨度>();
ary [ 1 ] = new float ();


假设您询问 C ++ std :: string

Quote:

字符串是一组字符对吗?

非常可能,但这是'依赖于实现'。





引用:

所有(静态)数组都必须在执行之前设置它的大小

Nope。 std :: string 可以增长,因此动态内存分配应该会发生。





Quote:

此外,当我创建一个字符串数组时,我将它的指针增加一个去到下一个字符串系统如何知道下一个地址?

数组的下一个项目与当前项目相邻。数组严格遵循线性记忆排列。它的内容是对 std :: string 对象的引用。





Quote:

另外,为什么我们不能拥有不同数据类型的数组?是不是因为内存大小?

是的,它(主要)是因为内存大小。





Quote:

如果我创建一个包含64位整数的结构A和一个包含两个32位整数的结构B,为什么我不能将它们放在同一个数组中?

因为是不同的类型。但是,您可以创建一个包含两个 struct union ,然后将union放在一个数组中。


是的,必须确定字符串大小。但是当你这样做时:



 char * example =Jack; 





字符串的大小由编译器自动设置。 示例是指针。



但是:



 char示例[ 5]; 
strcpy(例如,杰克);





这里你要确定字符串的大小。该字符串是一个静态数组。

你可以在不使用'strcpy'的情况下完成此任务。



  char 示例[ 5 ]; 
示例[ 0 ] = ' J' ;
示例[ 1 ] = ' a' ;
示例[ 2 ] = ' c' ;
示例[ 3 ] = ' k' ;
示例[ 4 ] = ' \ 0';





结果相同。这样做是为了确保字符串也是数组。


String is an array of chars right?
So like all(static)arrays it's size has to be set before execution
Where does this happen?
Also,when I crate an array of strings and I increase it's pointer by one to go to the next string how does the system knows the next address?

Also,why can't we have arrays of different data types?Is it because of memory size?
If I create a structure A containing a 64-bit integer and a structure B containing two 32-bit integers why can't I put them in the same array?

Thank you very much !

解决方案

Length of string is determined in runtime dynamically. String has length and capacity properties, when you define a new string it allocates extra space to create additional buffer in case you could expand this string.

For instance,

string str("abcdefgh");



length: 8
capacity: 15

If it had capacity of 8, adding any characters to the string would force a reallocation. By making the capacity larger than the actual string, this gives the user some buffer room to expand the string before reallocation needs to be done. Reallocation is expensive because, each character in the string has to be copied to the new memory.

And when you are creating an array of string, you are creating an array of pointers which holds start address of strings.

If you want to create array of different types you need to use array of void pointers like this;

void *ary[10];
ary[0] = new int();
ary[1] = new float();


Assuming you are asking about C++ std::string.

Quote:

String is an array of chars right?

Very likely, however that is 'implementation dependent'.


Quote:

So like all(static)arrays it's size has to be set before execution

Nope. std::string can grow, hence dynamic memory allocation should happen, sometime.


Quote:

Also,when I crate an array of strings and I increase it's pointer by one to go to the next string how does the system knows the next address?

The next item of the array is adjacent to the current one. An array strictly follow a linear memory arrangement. Its content is a reference to the std::string object.


Quote:

Also,why can't we have arrays of different data types?Is it because of memory size?

Yes, it is (mainly) because of memory size.


Quote:

If I create a structure A containing a 64-bit integer and a structure B containing two 32-bit integers why can't I put them in the same array?

Because the are diffrent types. However, you may create a union containing the two structs and then put the union in an array.


Yes, a string size has to be determined. But when you do this:

char *example = "Jack";



The size of the string is automatically set by the compiler. "example" is a pointer.

But:

char example[5];
strcpy(example, "Jack");



Here you're determining the size of the string. This string is a static array.
You can accomplish this without using 'strcpy' by the following.

char example[5];
example[0] = 'J';
example[1] = 'a';
example[2] = 'c';
example[3] = 'k';
example[4] = '\0';



The results are same. This is done to ensure you that strings are also arrays.


这篇关于如何确定字符串大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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