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

std::vector動的サイズの配列をカプセル化するシーケンス コンテナーです。

-- cppreference.com

これは、 C で使用するように動的に割り当てられたs の配列ではなく、 C++ で使用する必要があるものです。struct

これをコードに組み込む方法を次に示します(テストされていません)...

#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. typedef多くの場合、コードが読みやすくなります
  2. std::vector::push_back()インデックスを保持したり、サイズを過度に気にしたりする必要なく、コンテナの最後に追加します。
  3. イテレータ ( によって返されるものなどstd::vector::begin()) を使用すると、コンテナ内の要素に簡単にアクセスできます。この場合も、インデックスを保持する必要はありません。配列インデックスの代わりに反復子を使用する理由も参照してください。
  4. pointsこの実装は、ベースの実装のようにリークしmalloc()ません。
于 2013-01-03T12:54:21.493 に答える
3

ベクトルを調べる

#include <vector>

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

于 2013-01-03T12:38:34.507 に答える
2

それを見るとmalloc、intへのポインターを格納するのに十分なメモリを取得するために使用しています。malloc/を使用しないでfreeください。これは C++です。本当に必要な場合はneworを使用してください。delete

vectorありがたいことに、これは次のようにすべてを a で行うことができるため、メモリの動的割り当てについて心配する必要がない場合です。

// 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 に答える