gpt4 book ai didi

c++ - 在 C++ 中手动调整数组大小

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:20 25 4
gpt4 key购买 nike

<分区>

很抱歉,如果之前已经介绍过了。我知道如何做到这一点是 C 和 Java 但不是 C++。如果不使用包含使用 Vector 的预先存在的类,您将如何增加给定以下代码的数组的大小?

数组扩展和对数组的赋值发生在 push() 中,并用全部大写注释注明。

编辑:正如我在下面的评论中提到的,这是一个关于手动重新分配数组而不是使用 std::vector 或“动态数组”的问题。

行.h

#include <iostream>
#include "Point.h"

using namespace std;

class Line {
public:
Line();
virtual ~Line();

// TAKE IN NEW POINT, INCREASE THE ARRAY SIZE AND ADD NEW POINT TO THE END OF THE ARRAY
void push(const Point& p);

private:
unsigned int index; // size of "points" array
Point* points;

};

main.cpp

#include <iostream>
#include "Point.h"
#include "Line.h"

using namespace std;

int main() {

int x, y;
int size; // Some user defined size for the array
Line line;

Point a[size]; // Some points that are already filled

// Push the data in a[] to the variable "line"
for(int i = 0; i < size; i++){
// Increase array size of Point* points in variable line and add a[i] to the end of the array
line.push(points[i]);
}

return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com