2016-05-17 1 views
2

行列のベクトルまたは行ベクトルを10個の要素の配列に挿入する必要があります。私が行うことは、配列n x 10の行列の行として入力することです。マトリクスプッシュの場合、各配列は1行分増加します。繰り返し行列の各ステップは、あろう場合:配列を行列にpush_backする

[1]〜[10] .. [2] [10] .. [3] [10]

私用std::array<int 10>を使用してい配列の構築私はよくあなたの問題を理解している場合

+3

あなたが何かを試してみるのですか? – Garf365

答えて

1

あなたはここで次のコンテナstd::vector<std::array<int, 10>>

を使用することができます実証プログラム

です
#include <iostream> 
#include <iomanip> 
#include <array> 
#include <vector> 

int main() 
{ 
    const size_t N = 10; 
    std::vector<std::array<int, N>> matrix; 

    matrix.push_back({ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } }); 

    for (const auto &row : matrix) 
    { 
     for (int x : row) std::cout << std::setw(2) << x << ' '; 
     std::cout << std::endl; 
    }   

    std::cout << std::endl; 

    std::array<int, N> a = { { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 } }; 
    matrix.push_back(a); 

    for (const auto &row : matrix) 
    { 
     for (int x : row) std::cout << std::setw(2) << x << ' '; 
     std::cout << std::endl; 
    }   
} 

プログラムの出力は

0 1 2 3 4 5 6 7 8 9 

0 1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 
-1

一つの可能​​な解決策:

std::vector<std::vector<T>> my_matrix; 
.... 
std::array<T,10> my_array; 
... 
std::vector<T> temp; 
temp.reserve(10); 
std::copy(std::begin(my_array),std::end(my_array),std::back_insertor(temp)); 
my_matrix.emplace_back(std::move(temp)); 

またはより良いを:

std::vector<std::vector<T>> my_matrix; 
.... 
std::array<T,10> my_array; 
... 
my_matrix.emplace_back(std::begin(my_array),std::end(my_array)); 
+0

完璧!正常に動作します。今私はリコールのために私の行列をマットに変換しなければならないSVM(opencv)でトレーニングデータを設定する。手伝って頂けますか? SVMが使用するオブジェクトは次のとおりです。Mat trainingDataMat(4,2、CV_32FC1、trainingData); –

+0

このコードを使用できますか? \t 'cv :: Mat matMatrix.rows; + i) \t(int j = 0; j (i、j)= matrix.at(i).at(j); ' –

関連する問題