2016-08-04 110 views
0

我有一個在dlib中定義的colume向量。我怎樣才能將其轉換爲std :: vector?如何將dlib中的矩陣轉換爲std :: vector

typedef dlib::matrix<double,0,1> column_vector; 
column_vector starting_point(4); 
starting_point = 1,2,3,4; 
std::vector x = ?? 

感謝

回答

3

方法有很多種。你可以通過for循環來複制它。或者使用帶迭代器的std :: vector構造函數:std::vector<double> x(starting_point.begin(), starting_point.end())

+0

謝謝。但不應該是標準::矢量 x(starting_point.begin(),starting_point.end())? – colddie

+0

糟糕,沒錯。 –

0

這將是你通常遍歷矩陣的方式(不要緊,如果矩陣只有1列):

// loop over all the rows 
for (unsigned int r = 0; r < starting_point.nr(); r += 1) { 
    // loop over all the columns 
    for (unsigned int c = 0; c < starting_point.nc(); c += 1) { 
     // do something here 
    } 
} 

那麼,你爲什麼不遍歷您的列向量和介紹每個值都變成新的std::vector?這裏是一個完整的例子:

#include <iostream> 
#include <dlib/matrix.h> 

typedef dlib::matrix<double,0,1> column_vector; 

int main() { 
    column_vector starting_point(4); 
    starting_point = 1,2,3,4; 

    std::vector<double> x; 

    // loop over the column vector 
    for (unsigned int r = 0; r < starting_point.nr(); r += 1) { 
     x.push_back(starting_point(r,0)); 
    } 

    for (std::vector<double>::iterator it = x.begin(); it != x.end(); it += 1) { 
     std::cout << *it << std::endl; 
    } 
}