再次动态内存分配 [英] Again Dynamic memory allocation

查看:76
本文介绍了再次动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我不太确定如何使用新的和删除整个数组

动态。

实际上我想要的是,用户在一个char数组中输入一个char。

每当他输入一个char时,他就会创建一个新的数组(之前的大小+

1),然后,他将旧数组的内容复制到新数组

,并在最后添加新输入并删除旧数组。我想

他每次都必须将指针传递给数组。

例如。

输入:a

输出

输入:b

输出:ab

输入c:

输出abc。

任何线索?????????????

Hi all

Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.
For example.
input: a
output a
input: b
output: a b
input c:
output a b c .
Any clues??????????????

推荐答案

2月16日,上午10:45,whitehatmira ... @ gmail.com

< whitehatmira ... @ gmail.comwrote:
On Feb 16, 10:45 am, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:

大家好


我不太确定如何使用新的和删除整个数组

动态。

实际上我想要这样,用户在一个char数组中输入一个char。

每当他输入一个char,他就会创建一个新的数组(之前的大小+

1),然后他将旧数组的内容复制到新数组

并在末尾添加新输入并删除旧数组。我猜

他必须每次都将指针传递给数组。
Hi all

Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.



[snip]

什么阻止你使用std :: vector吗?

/ Peter

[snip]
What prevents you from using a std::vector?

/Peter


peter koch写道:
peter koch wrote:

2月16日上午10点45分,whitehatmira ... @ gmail.com

< whitehatmira ... @ gmail.comwrote :
On Feb 16, 10:45 am, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:



--snip--

--snip--


>其实我想要的是,用户在单个char数组中输入char。
>Actually i want that, a user inputs a char in a single char array.



--snip--

--snip--


> ;>
>>



[snip]

什么阻止你使用std :: vector?


/ Peter

[snip]
What prevents you from using a std::vector?

/Peter



为什么不使用std :: string? imho它对字符串很有效。

如果使用宽字符使用std :: wstring。


ismo

Why not use std::string ? imho it works quite well for strings.
If working with wide chars use std::wstring.

ismo


>什么阻止你使用std :: vector?
>What prevents you from using a std::vector?

>为什么不使用std :: string?
>Why not use std::string ?



Eric Nagler在c ++上的书,在开头没有介绍std :: vector或

字符串。

我知道这是一个蹩脚的借口但是,在

开头的一个问题中,他们还没有引入向量,他们要求使用

new和删除。也许只是为了让程序员重新发明轮子,

然后告诉他有一些更简单的东西,以便他能够理解美丽,复杂或其他什么...... 。


:)

The book by Eric Nagler on c++, doesnt introduce std::vector or
string, in the beginning.
I know thats a lame excuse but, in one of the problems in the
beginning, where they have not yet introduced vectors, they ask to use
new and delete. Maybe just to let the programmer reinvent the wheel,
and then show him that there is something simpler so that he
understands the beauty, complexity or whatever......

:)


这篇关于再次动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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