[英]c++ dynamic array of structures
Could you help me organize a dynamic array of points? 你能帮我组织一系列动态点吗?
I have coped with a dynamic array of integers. 我已经处理了一个动态的整数数组。 But I don't know how to organize it with structures. 但我不知道如何用结构来组织它。
Here is my code so far... 这是我到目前为止的代码......
#include "stdafx.h"
#include <cstdlib>;
#include <iostream>;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0; // Array index.
struct Point
{
int x;
int y;
};
size_t n = sizeof(int);
int * points = static_cast<int*>(malloc(n));
char command;
do
{
cout << "n - Create new point." << endl;
cout << "q - Quit." << endl;
cout << "Input a new command: ";
cin >> command;
if (command == 'n')
{
points[i] = 1;
i++;
/* points[i] = new Point();
points[i].x = 1;
points[i].y = 1; */
// cout<<"("<<point1.x<<","<<point1.y<<")";
}
else if (command == 'q')
{
for (int j = 0; j < i; j++)
cout << points[j] <<endl;
system("pause");
return 0;
}
else
{
cout << "Please, enter a correct command." << endl << endl << endl;
}
} while (true);
system("pause");
return 0;
}
look into vectors 看看向量
#include <vector>
http://www.mochima.com/tutorials/vectors.html http://www.mochima.com/tutorials/vectors.html
std::vector
is a sequence container that encapsulates dynamic size arrays.std::vector
是一个封装动态大小数组的序列容器。
-- cppreference.com - cppreference.com
This is what you should use in C++ , rather than a dynamically-allocated array of struct
s as you would in C . 这是你应该在C ++中使用的,而不是像在C中那样动态分配的struct
数组。
Here is how you can start to incorporate this into your code (untested)... 以下是如何开始将其合并到您的代码中(未经测试)...
#include <vector>
#include <iostream>
struct Point
{
int x;
int y;
};
typedef std::vector<Point> PointVector;
int main()
{
using std::cout;
using std::cin;
using std::endl;
PointVector points;
char command = 0;
do
{
cout << "n - Create new point." << endl;
cout << "q - Quit." << endl;
cout << "Input a new command: ";
cin >> command;
if (command == 'n')
{
Point new_point;
new_point.x = 1;
new_point.y = 1;
points.push_back(new_point);
cout << "(" << new_point.x << "," << new_point.y << ")\n";
}
else if (command == 'q')
{
for (PointVector::iterator it = points.begin(), end = points.end();
it != end;
++it)
{
cout << "(" << it->x << "," << it->y << ")\n";
}
break;
}
else
{
cout << "Please, enter a correct command." << endl;
}
} while(true);
}
typedef
s often make code more readable typedef
经常使代码更具可读性 std::vector::push_back()
adds to the end of the container, without you needing to keep an index or be overly-concerned about size. std::vector::push_back()
添加到容器的末尾,无需保留索引或过度关注大小。 std::vector::begin()
) make it easy to access the elements within the container, again without needing to keep an index. 迭代器(例如由std::vector::begin()
返回的迭代器)使得访问容器中的元素变得容易,同样无需保留索引。 See also Why use iterators instead of array indices? 另请参见为什么使用迭代器而不是数组索引? points
as your malloc()
based implementation would do. 这个实现不会像基于malloc()
的实现那样泄漏points
。 By the look of it, you are using malloc
to grab enough memory to store a pointer to an int. 通过它看,你正在使用malloc
来获取足够的内存来存储指向int的指针。 Dont use malloc
/ free
. 不要使用malloc
/ free
。 This is C++, use new
or delete
if you really have to. 这是C ++,如果你真的需要,可以使用new
或delete
。
Thankfully, this is an occasion where you don't have to worry about dynamically allocating memory as you can do all this in a vector
as follows: 值得庆幸的是,这是一个你不必担心动态分配内存的场合,因为你可以在vector
完成所有这些操作,如下所示:
// First create a vector of points.
std::vector<Points> points;
// Now create the Point (i'm assuming you were going to read
// it from the input stream here.)
cin >> x >> y;
Point p;
p.x = x;
p.y = y;
// And add it to the vector.
points.push_back( p );
It's that simple, you don't need to worry about increasing the capacity of the points vector or clearing it down at the end. 就这么简单,您不必担心增加点矢量的容量或在结束时将其清除。
You can use vectors as @iedoc suggested: 您可以使用@iedoc建议的向量:
#include <iostream>;
#include <vector>;
using namespace std;
struct SPoint
{
int X;
int Y;
};
int main()
{
vector<SPoint> points;
char command;
do
{
cout << "n - Create new point." << endl;
cout << "q - Quit." << endl;
cout << "Input a new command: ";
cin >> command;
if (command == 'n')
{
int x = 0;
int y = 0;
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
SPoint point = { x, y};
points.push_back(point);
}
else if (command == 'q')
{
for (int i = 0; i < points.size(); ++i)
{
cout << "(" << points[i].X << "," << points[i].Y << ")";
}
return 0;
}
else
{
cout << "Please, enter a correct command."<<endl<<endl<<endl;
}
}
while(true);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.