これが私が抱えている問題です。うまく説明できなかったり、コードの品質が悪い場合でも、私を殴らないでください。これまでのところ、C++は約2週間しか実行していません。
説明:一連の点(集合を円弧と呼びましょう)の座標(xとyのみ)を含む構造(構造は最良の決定ではないかもしれませんが、どこかから始めなければなりません)を構築したいと思います。 id(および場合によっては他のフィールド)。各セット(アーク)には、さまざまな数のポイントを含めることができます。セット(アーク)の各ポイントをクラスとして実装しました。アーク構造には、このクラスのさまざまなインスタンスが(他のものとともに)ベクトルに含まれています。
円弧構造の例:
Struc1:
Id(int)1
xY(ベクトル)(0; 0)(1; 1)(2; 2)
Struc2:
Id(int)2
xY(ベクトル)(1; 1)(4; 4)
問題:
円弧構造内の要素にアクセスする方法がわかりません。たとえば、Id 1を使用して構造内の2番目の点の座標にアクセスする必要がある場合、必要になりますが、Struc1.xY[1]
これはコードとして機能しません。 (下)スタンド。構造体内の値を出力する方法を説明するこの投稿を見つけましたが、これらの要素にアクセスして(後で)条件付きでこれらの座標を編集する必要があります。これをどのように実施できますか?
私の試み:(編集済み)
#include <cmath>
#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
using namespace std;
class Point
{
public:
Point();
~Point(){ }
void setX (int pointX) {x = pointX; }
void setY (int pointY) {y = pointY; }
int getX() { return x; }
int getY() { return y; }
private:
int x;
int y;
};
Point::Point()
{
x = 0;
y = 0;
}
struct arc {
int id;
vector<Point> xY;
};
int main(){
arc arcStruc;
vector<Point> pointClassVector;
int Id;
int X;
int Y;
// other fields go here
arc *a;
int m = 2; // Just create two arcs for now
int k = 3; // each with three points in it
for (int n=0; n<m; n++){
a = new arc;
Id = n+1;
arcStruc.id = Id;
Point pt;
for (int j=0; j<k; j++){
X = n-1;
Y = n+1;
pt.setX(X);
pt.setY(Y);
arcStruc.xY.push_back(pt);
}
}
for (vector<Point>::iterator it = arcStruc.xY.begin(); it != arcStruc.xY.end(); ++it)
{
cout << arcStruc.id.at(it);
cout << arcStruc.xY.at(it);
}
delete a;
return 0;
}