C++:如何将更改应用于动态数组交叉函数?
C++: How to apply changes to dynamic array cross functions?
我正在尝试扩展名为 Coffee(Coffee * brands = new Coffee[sizeOf];
) 的 class 对象的动态数组,通常我只会创建一个临时动态数组,其大小比初始数组大一个数组的大小 - 我将它们复制到临时文件中,在末尾添加新的,删除旧的,重新创建但扩展,然后从临时文件中将所有内容复制回它。
但是现在,我必须在数组必须位于的主函数之外执行此操作(是的,这是一项任务,那是一项棘手的任务),所以我将数组和大小作为参数传入,然后开始制作临时数组并复制所有内容,在末尾添加新对象。
这就是我迷路的地方;如果我想通过子函数从 main 中删除数组,如何通过相同的函数重新替换 it/make 它?这对我来说听起来很荒谬,但我不知道如何获得所需的额外缺口,或者只是将 +1 添加到大小变量并将新对象的信息以某种方式复制到最后一个插槽?
这个问题不是重复的,因为我已经检查了其他问题的一些其他答案,但找不到相同的答案,因为这是为了让改变发生从另一个专门使用动态数组的函数,我看到的每个答案(比如这个:How to expand an array dynamically in C++? {like in vector })要么只处理主函数,要么建议使用向量(这不帮助我,因为我必须使用动态数组 ),我被一些不相关的细节搞糊涂了,比如 *(p+i) = i
.
此外,有人可以解释一下这里的范围差异以及为什么您的解决方案与它们协调一致吗?我忽略或错过了什么?
一些代码:
#include <iostream>
#include <string>
#include "Coffee.h"
using namespace std;
void addCoffee(Coffee c[], int &amount);
void showAllCoffees(Coffee c[], int amount);
int main()
{
int sizeOf = 5;
Coffee * brand = new Coffee[sizeOf];
addCoffee(brand, &sizeOf);
showAllCoffees(brand, sizeOf); //nevermind this part,
//just added it so it makes more sense
delete [] brand;
return 0;
}
void addCoffee(Coffee c[], int &amount){
string name;
amount++;
Coffee * temp = new Coffee[amount];
for(int i=0;i<amount-1;i++){
temp[i] = c[i];
}
cout<<"What is the coffee's name?: ";
cin>>name;
temp[amount-1].setName(name);
/*THIS IS WHERE I'M NOT SURE WHAT TO DO*/
delete [] temp;
}
I'm trying to expand a dynamic array of objects of a class called
Coffee( Coffee * brands = new Coffee[sizeOf]; ), usually I would just
make a temp dynamic array whose size is one bigger than the initial
array's size - I copy them over to temp, add the new one at the end,
delete the old one, make it anew but expanded, and copy back
everything to it from temp.
every answer I've seen(such as this one: How to expand an array
dynamically in C++? {like in vector } ) deals with either the
main-function only, or vectors are suggested(which doesn't help me
since I have to use dynamic arrays)
我认为你真的需要一个std::vector
。
那些是个动态数组。
更新:
好的,这可能是您被迫创建自己的动态数组的作业。关于扩展和添加新项目的快速提示:
通常扩展是由 reallocating/growing 现有数组完成的(但这可能不在此赋值范围内)。但是,如果您每次都通过创建一个新数组来扩展,最好将数组增大到更大的大小(并跟踪分配的大小)。这样你就不必为每个 add() 创建一个新的。
通常这是通过扩大 2 的幂大小来完成的:16 项 -> 32 -> 64 --> 1024,从那时起每次可能都是 1024。
因此,最好创建您自己的 dynamic-array class(您可以在其中跟踪分配的大小、项目数量等)。
编辑:查看 Jerry Coffin's blog post 以获得一个很好的例子。
在 addCoffee
的末尾,不要 delete[] temp
- 那是你的新数据存储的地方;改为 delete[] c
,然后 return temp
并将函数 return 类型更改为 Coffee*
.
主要说 brand = addCoffee(...)
所以它保存 return 值。
综上所述,您需要考虑一些事情 - 尽管前 5 个元素中没有数据,但您硬核了初始大小 5。
您必须传递对函数指针的引用:
void addCoffee(Coffee *&c, int &amount){
// ^^ c is input and output
string name;
amount++;
Coffee * temp = new Coffee[amount];
for(int i=0;i<amount-1;i++){
temp[i] = c[i];
}
cout<<"What is the coffee's name?: ";
cin>>name;
temp[amount-1].setName(name);
delete [] c; // delete old array
c = temp; // assign new array pointer to c
}
但我建议使用 std::vector
而不是动态数组。
void addCoffee( std::vector<Coffee> &c ){
cout<<"What is the coffee's name?: ";
cin>>name;
c.push_back( Coffee( name ) );
}
您正在传递指向 addCoffee
的指针。由于您希望能够修改该指针,因此您可能希望通过引用而不是值来传递它。
void addCoffee(Coffee *&c, int &amount)
然后在 addCoffee
中,在将所有元素从 c
复制到 temp
之后(并将新项目添加到 temp
),想要交换指针 temp
和 c
,然后删除旧数组。
如果我按照这个一般顺序做某事,我可能会定义一个结构来保存指针和当前大小:
template <class T>
struct dynamic_array {
T *array;
size_t size;
};
这几乎是向 std::vector
提供的一小步。
我正在尝试扩展名为 Coffee(Coffee * brands = new Coffee[sizeOf];
) 的 class 对象的动态数组,通常我只会创建一个临时动态数组,其大小比初始数组大一个数组的大小 - 我将它们复制到临时文件中,在末尾添加新的,删除旧的,重新创建但扩展,然后从临时文件中将所有内容复制回它。
但是现在,我必须在数组必须位于的主函数之外执行此操作(是的,这是一项任务,那是一项棘手的任务),所以我将数组和大小作为参数传入,然后开始制作临时数组并复制所有内容,在末尾添加新对象。
这就是我迷路的地方;如果我想通过子函数从 main 中删除数组,如何通过相同的函数重新替换 it/make 它?这对我来说听起来很荒谬,但我不知道如何获得所需的额外缺口,或者只是将 +1 添加到大小变量并将新对象的信息以某种方式复制到最后一个插槽?
这个问题不是重复的,因为我已经检查了其他问题的一些其他答案,但找不到相同的答案,因为这是为了让改变发生从另一个专门使用动态数组的函数,我看到的每个答案(比如这个:How to expand an array dynamically in C++? {like in vector })要么只处理主函数,要么建议使用向量(这不帮助我,因为我必须使用动态数组 ),我被一些不相关的细节搞糊涂了,比如 *(p+i) = i
.
此外,有人可以解释一下这里的范围差异以及为什么您的解决方案与它们协调一致吗?我忽略或错过了什么?
一些代码:
#include <iostream>
#include <string>
#include "Coffee.h"
using namespace std;
void addCoffee(Coffee c[], int &amount);
void showAllCoffees(Coffee c[], int amount);
int main()
{
int sizeOf = 5;
Coffee * brand = new Coffee[sizeOf];
addCoffee(brand, &sizeOf);
showAllCoffees(brand, sizeOf); //nevermind this part,
//just added it so it makes more sense
delete [] brand;
return 0;
}
void addCoffee(Coffee c[], int &amount){
string name;
amount++;
Coffee * temp = new Coffee[amount];
for(int i=0;i<amount-1;i++){
temp[i] = c[i];
}
cout<<"What is the coffee's name?: ";
cin>>name;
temp[amount-1].setName(name);
/*THIS IS WHERE I'M NOT SURE WHAT TO DO*/
delete [] temp;
}
I'm trying to expand a dynamic array of objects of a class called Coffee( Coffee * brands = new Coffee[sizeOf]; ), usually I would just make a temp dynamic array whose size is one bigger than the initial array's size - I copy them over to temp, add the new one at the end, delete the old one, make it anew but expanded, and copy back everything to it from temp.
every answer I've seen(such as this one: How to expand an array dynamically in C++? {like in vector } ) deals with either the main-function only, or vectors are suggested(which doesn't help me since I have to use dynamic arrays)
我认为你真的需要一个std::vector
。
那些是个动态数组。
更新:
好的,这可能是您被迫创建自己的动态数组的作业。关于扩展和添加新项目的快速提示:
通常扩展是由 reallocating/growing 现有数组完成的(但这可能不在此赋值范围内)。但是,如果您每次都通过创建一个新数组来扩展,最好将数组增大到更大的大小(并跟踪分配的大小)。这样你就不必为每个 add() 创建一个新的。
通常这是通过扩大 2 的幂大小来完成的:16 项 -> 32 -> 64 --> 1024,从那时起每次可能都是 1024。
因此,最好创建您自己的 dynamic-array class(您可以在其中跟踪分配的大小、项目数量等)。
编辑:查看 Jerry Coffin's blog post 以获得一个很好的例子。
在 addCoffee
的末尾,不要 delete[] temp
- 那是你的新数据存储的地方;改为 delete[] c
,然后 return temp
并将函数 return 类型更改为 Coffee*
.
主要说 brand = addCoffee(...)
所以它保存 return 值。
综上所述,您需要考虑一些事情 - 尽管前 5 个元素中没有数据,但您硬核了初始大小 5。
您必须传递对函数指针的引用:
void addCoffee(Coffee *&c, int &amount){
// ^^ c is input and output
string name;
amount++;
Coffee * temp = new Coffee[amount];
for(int i=0;i<amount-1;i++){
temp[i] = c[i];
}
cout<<"What is the coffee's name?: ";
cin>>name;
temp[amount-1].setName(name);
delete [] c; // delete old array
c = temp; // assign new array pointer to c
}
但我建议使用 std::vector
而不是动态数组。
void addCoffee( std::vector<Coffee> &c ){
cout<<"What is the coffee's name?: ";
cin>>name;
c.push_back( Coffee( name ) );
}
您正在传递指向 addCoffee
的指针。由于您希望能够修改该指针,因此您可能希望通过引用而不是值来传递它。
void addCoffee(Coffee *&c, int &amount)
然后在 addCoffee
中,在将所有元素从 c
复制到 temp
之后(并将新项目添加到 temp
),想要交换指针 temp
和 c
,然后删除旧数组。
如果我按照这个一般顺序做某事,我可能会定义一个结构来保存指针和当前大小:
template <class T>
struct dynamic_array {
T *array;
size_t size;
};
这几乎是向 std::vector
提供的一小步。