动态分配 [英] Dynamic allocation

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

问题描述

大家好


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

动态。

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

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

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

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

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


例如。

输入:a

输出一个

输入:b

输出:ab

输入c:

输出abc 。


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

解决方案

" WH ************* @ gmail.com" < wh ************* @ gmail.comwrote

新闻:11 ****************** ****@a75g2000cwd.googlegr oups.com:


大家好


我不太确定如何使用new和delete for a array $

动态。

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

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

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

并添加最后的新输入并删除旧数组。我猜

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


例如。

输入:a

输出一个

输入:b

输出:ab

输入c:

输出abc 。


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



忘了阵列。查找std :: vector。


wh *************@gmail.com 写道:


大家好


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

动态。

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

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

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

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

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


例如。

输入:a

输出一个

输入:b

输出:ab

输入c:

输出abc 。


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



char * array = new char [sizeof_of_array];


delete []数组;


但是Andre正确使用std :: vector或std :: string。忘记记忆

分配细节并学习如何编程。


john


试试:


#include< vector>

#include< iostream>

使用命名空间std;


vector< charvc;

char my_char;

for(;;;)

{

cin > my_char;

vc.push_back(my_char);

for(int i = 0; i< my_char.size(); i ++)

{

cout<< my_char [i]<<" " ;;

}

cout<< endl;

}


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??????????????

解决方案

"wh*************@gmail.com" <wh*************@gmail.comwrote in
news:11**********************@a75g2000cwd.googlegr oups.com:

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??????????????

Forget arrays. Look up std::vector.


wh*************@gmail.com wrote:

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??????????????

char* array = new char[sizeof_of_array];

delete[] array;

But Andre is right use std::vector or std::string. Forget memory
allocation details and learn how to program.

john


Try that:

#include<vector>
#include<iostream>
using namespace std;

vector<charvc;
char my_char;
for(;;;)
{
cin >my_char;
vc.push_back(my_char);
for(int i = 0; i < my_char.size(); i++)
{
cout << my_char[i] <<" ";
}
cout << endl;
}


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

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