3

你能帮我组织一个动态的点数组吗?

我已经处理了一个动态的整数数组。但我不知道如何用结构来组织它。

到目前为止,这是我的代码...

#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;
}
4

4 回答 4

3

查看矢量图

#include <vector>

http://www.mochima.com/tutorials/vectors.html

于 2013-01-03T12:38:34.507 回答
3

std::vector是一个封装动态大小数组的序列容器。

-- cppreference.com

这是您应该在C++中使用的,而不是像在Cstruct中那样动态分配的 s数组。

以下是您如何开始将其合并到您的代码中(未经测试)...

#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);
}

关键点:

  1. typedefs 通常使代码更具可读性
  2. std::vector::push_back()添加到容器的末尾,而无需保留索引或过度关注大小。
  3. 迭代器(例如 由 返回的迭代器std::vector::begin())使访问容器中的元素变得容易,同样无需保留索引。另请参阅为什么使用迭代器而不是数组索引?
  4. 此实现不会points像您malloc()的基于实现那样泄漏。
于 2013-01-03T12:54:21.493 回答
2

从外观上看,您正在使用malloc获取足够的内存来存储指向 int 的指针。不要使用malloc/ free。这是 C++,使用new或者delete如果你真的需要。

值得庆幸的是,在这种情况下,您不必担心动态分配内存,因为您可以按以下方式完成所有这些操作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 );

就这么简单,你不需要担心增加点向量的容量或者最后清除它。

于 2013-01-03T13:00:46.627 回答
1

您可以按照@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;
}
于 2013-01-03T12:55:45.890 回答