用新的方式生成一个数组 [英] grow an array with new

查看:54
本文介绍了用新的方式生成一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我分配一个数组

-----

a = new int(10);

-----

之后我希望将数组增加到20个整数

我必须分配第二个数组并将第一个数组复制到第二个?

没有"较小代码"进场? (像realloc这样的东西)


我不想要长时间的回答。我想要是或否或否 ;-)


谢谢

If I allocate an array with
-----
a = new int(10);
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
There is no "smaller-code" approach? (Something like realloc)

I dont want long answer. I want "YES" or "NO" ;-)

Thanks

推荐答案

"< - Chameleon - >" escribió:
"<- Chameleon ->" escribió:
如果我用
分配一个数组-----
a = new int(10);


你正在分配一个值为10的int。


你可能意味着:a = new int [10];

-----
我希望将数组增加到20个整数
我必须分配第二个数组并将第一个数组复制到第二个?


是。

没有小代码进场? (像realloc一样)
If I allocate an array with
-----
a = new int(10);
You are allocating a single int with value 10.

You probaly mean: a= new int [10];
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
Yes.
There is no "smaller-code" approach? (Something like realloc)




使用std :: vector。


问候。



Use std::vector.

Regards.

< br>

< - 变色龙 - >在新闻中写道:bj ********** @ nic.grnet.gr:
<- Chameleon -> wrote in news:bj**********@nic.grnet.gr:
如果我用
分配数组-----
a = new int(10);
-----
我希望将数组增长到20个整数
我必须分配第二个数组并将第一个数组复制到第二个?
没有小代码进场? (像realloc这样的东西)

我不想要长时间的回答。我想要是或否或否 ; - )
If I allocate an array with
-----
a = new int(10);
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
There is no "smaller-code" approach? (Something like realloc)

I dont want long answer. I want "YES" or "NO" ;-)




NO


Rob。

-
http://www.victim-prime.dsl.pipex.com/


Rob Williscroft写道:
Rob Williscroft wrote:
< - Chameleon - >在新闻中写道:bj ********** @ nic.grnet.gr:
<- Chameleon -> wrote in news:bj**********@nic.grnet.gr:
如果我用
分配数组-----
a = new int(10);
-----
我希望将数组增长到20个整数
我必须分配第二个数组并将第一个数组复制到第二个?
没有小代码进场? (像realloc这样的东西)

我不想要长时间的回答。我想要是或否或否 ; - )
If I allocate an array with
-----
a = new int(10);
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
There is no "smaller-code" approach? (Something like realloc)

I dont want long answer. I want "YES" or "NO" ;-)



没有

Rob。



NO

Rob.



我很好奇:我们不能做类似这样的事情:


#include< iostream>


int main()

{

int * n = new int [10];

for(int i = 0; i< 10; i ++)

n [i] = i;


int * m = n + 10;

m = new int [10];

for(int i = 10; i< 20; i ++)

n [i] = i;

for(int i = 0; i< 20; i ++)

std :: cout<< n [i]<< '''';

std :: cout<< ''\\ n';;

}


这给了我以下输出:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

问候,

Sumit。


I''m curious: can''t we do something like this:

#include <iostream>

int main()
{
int* n = new int[10];
for(int i=0; i<10;i++)
n[i] = i;

int* m = n+10;
m = new int[10];
for(int i=10; i<20;i++)
n[i] = i;
for (int i=0; i<20; i++)
std::cout << n[i] << '' '';
std::cout << ''\n'';
}

This gave me the following as output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Regards,
Sumit.

这篇关于用新的方式生成一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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