2017-06-20 150 views
-2

我是新來的C++,並試圖正確設置矩陣。我的數據將有未知數量的行,但將有6列,我正在考慮使用vector>或boost multiarray軟件包。我可以設置一些類似的東西:C++ vector <vector <double>>使用typename別名

template<size_t t> 

using Matrix <t> = vector<vector<double>> m(t, vector<double>(6)) 

或將這不工作/不適合/不建議?

+0

讓6個陣列 –

+0

的矢量爲什麼你不只是嘗試,如果它[工程](http://coliru.stacked-crooked.com/ A/bc9a9e3295e3dd5b)? –

+0

我覺得你的編譯器可能不太喜歡這個語法。模板是相當編譯時間的事情,所以很可能不會讓你做任何動態的事情。 –

回答

0

你可以做類似如下:

#include <iostream> 
#include <array> 
#include <vector> 

template< typename T, size_t n > 
using Matrix = std::vector< std::array< T, n > >; 

typedef Matrix< double, 6 > SpecificMatrix; 

int main() 
{ 
    SpecificMatrix my_matrix(10); 
    double x = 0; 

    for (auto &a : my_matrix) 
    for (auto &b : a) 
     b = (x += 1.0); 

    std::cout << "My thingy:" << std::endl; 

    for (auto &a : my_matrix) 
    { 
    for (auto &b : a) 
     std::cout << b << " "; 

    std::cout << std::endl; 
    } 

    return 0; 
} 
相關問題