'row'または1Dベクトルの値を定義し、その行を2Dベクトルにpushしようとしています。私はエラーをスローしないで、動作するようにも見えないいくつかの異なるものを試しました。以下のコード:Push_back 1次元ベクトルを2次元ベクトル配列に
#include <vector>
#include <iostream>
using std::vector;
#define HEIGHT 5
#define WIDTH 3
// 2D VECTOR ARRAY EXAMPLE
int main() {
vector<vector<double> > array2D;
vector<double> currentRow;
// Set up sizes. (HEIGHT x WIDTH)
// 2D resize
array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i)
{
array2D[i].resize(WIDTH);
}
// Try putting some values in
array2D[1][2] = 6.0; // this works
array2D[2].push_back(45); // this value doesn't appear in vector output. Why?
// 1D resize
currentRow.resize(3);
// Insert values into row
currentRow[0] = 1;
currentRow[1] = 12.76;
currentRow[2] = 3;
// Push row into 2D array
array2D.push_back(currentRow); // this row doesn't appear in value output. Why?
// Output all values
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
std::cout << array2D[i][j] << '\t';
}
std::cout << std::endl;
}
return 0;
}
明らかに私はあなたにアップアップをするほどの評判はない。しかし、ありがとう! – ProGirlXOXO