ベクトルを使用して2D配列を作成します。しかし、これを行うと、セグエラーが発生します。誰でも私が間違っていることとこの問題の可能な解決策を説明できますか?.
私はゲッターとセッターに対処したくないので、すべてを公開しました。 2D配列の概念を明確にしたい。
#include <iostream>
#include <vector>
using namespace std;
class point
{
public:
point():x(0),y(0){}
~point(){}
point(float xx,float yy):x(xx),y(yy){}
float x,y;
};
int main()
{
vector<vector<point> > a; // 2D array
point p(2,3);
a[0][0] = p; // error here
return 0;
}
ベクターが空です。したがって、[0][0]
は使用できません。
宣言方法は次のとおりです。
a.Push_back(vector<point>());
a[0].Push_back(p);
最初から持っているアイテムの数がわかっている場合は、次のことができます。
vector<vector<point> > a(10, vector<point>(10));
これは、10ポイントを含む10個のベクトルを含むベクトルです。その後、使用することができます
a[4][4] = p;
ただし、ベクトルのベクトルを使用すると混乱を招くと思います。配列が必要な場合は、uBLASの使用を検討してください http://www.boost.org/doc/libs/1_41_0/libs/numeric/ublas/doc/index.htm
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
matrix<double> m (3, 3);
for (unsigned i = 0; i < m.size1 (); ++ i)
for (unsigned j = 0; j < m.size2 (); ++ j)
m (i, j) = 3 * i + j;
std::cout << m << std::endl;
}
別の提案があります。あなたが達成しようとしていることは以前に行われており、 Boost Multi-Array 内で見つけることができます。
空のベクトルのベクトルを作成し、要素を追加せずに最初の要素を間接参照しようとしました。
ベクトルは(一部の)連想配列のようには機能しません。この配列では、欠落している値にアクセスしようとすると、コレクションに追加されます。適切な形式のベクターコンストラクターを使用するか、Push_backを使用してアクセスする前に、ベクターに適切な数のエントリがあることを確認する必要があります。
2D配列をうまく作成しています。問題は、それを作成するとき、それは空の配列であるということです-それはまだまったくポイントを保持していません。実際にポイントを作成する前にse [0] [0]のポイントを試みます。通常、新しい要素をベクターに追加するには、resize()
を使用してベクターのサイズを設定するか、Push_back()
を使用してアイテムを1つずつ追加します。この場合、後者はおそらく少し不器用です-点のベクトルのベクトルがあるので、点のベクトルを作成し、そのベクトルに点をプッシュし、次にそのベクトルを配列にプッシュする必要があります。
最も簡単な方法は、次のようにresize()
メソッドを使用することです。
vector <vector<int>> v;
cin>>n>>m; //n is rows and m is columns
v.resize(n,vector<int>(m));
for(i=0;i<n;i++) // inserts elements into the vector v
for(j=0;j<m;j++)
cin>>v[i][j];
for(i=0;i<n;i++) //accesses elements of vector v
for(j=0;j<m;j++)
cout<<v[i][j]<<" ";
それを機能させることができた。どこかから「typedef」のアイデアを拾いました。以下のコードを試してください、それは動作します:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main()
{
int i = 0;
int j = 0;
///////////////////////////////////////////////////////////
typedef vector<string> vecRow;
typedef vector<vecRow> vecCol;
vecRow vr;
vecCol vc;
///////////////////////////////////////////////////////////
// Assigning string elements to the 2d array
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
vr.Push_back("string ["+to_string(i)+"]["+to_string(j)+"]");
}
vecRow vr_temp = vecRow(vr);
vc.Push_back(vr_temp);
vr.clear();
}
///////////////////////////////////////////////////////////
// Printing back the elements from the 2D array
for(auto element : vc)
{
for(unsigned int ictr = 0;ictr < element.size() ; ictr++)
{
cout<<element[ictr]<<"\t";
}
cout<<endl;
}
getchar();
return 0;
}
VectorMatrix [] []は、次のようにベクトルのマトリックスであると定義できます。
クラス:
class vectorMatrix
{
std::vector<object> **cell;
int columns;
int rows;
public:
vectorMatrix(int columns, int rows);
virtual ~vectorMatrix();
void addCellAt(int row, int column, const object& entry);
virtual std::vector<object>* getCell(int row, int column);
void clearMatrix();
};
コンストラクターを定義します。
vectorMatrix::vectorMatrix(int columns, int rows)
{
this->columns = columns;
this->rows = rows;
cell = new std::vector<object>* [columns];
for (int i = 0; i < columns; i++)
{
cell[i] = new std::vector<object>[rows];
}
}
エントリを追加する方法:
void vectorMatrix::addCellAt(int row, int column, const object& entry)
{
cell[channel][timeSlot].Push_back(entry);
}
特定の行と列のベクトルへのポインターを取得します。
std::vector<object>* vectorMatrix::getCell(int row, int column)
{
return &cell[row][column];
}
すべてのマトリックスをクリアする:
void vectorMatrix::clearMatrix()
{
for (int tmpRow = 0; tmpRow < columns; tmpRow ++)
{
for(int tmpColumn = 0; tmpColumn < rows; tmpColumn ++)
{
cell[tmpRow][tmpColumn].clear();
}
}
}
resize()
;を使用できます。たとえば、ここでa
のサイズを100 x 200配列に変更します。
vector<vector<point> > a; // 2D array
a.resize(100);
for_each(a.begin(),a.end(),[](vector<point>& v){v.resize(200);});
point p(2,3);
a[0][0] = p; // ok now