我有以下的二维向量/矩阵 X
和一个向量 Y
:
std::vector<double> Y;
unsigned int ctr=2;
std::vector<std::vector<double> >X(ctr,Y);
我现在想创建X的转置矩阵,即Xtrans,因此将其声明如下:
std::vector<std::vector<double> >Xtrans(Y,ctr);
但是它给我以下编译错误:
test.cpp:128:58: error: no matching function for call to ‘std::vector<std::vector<double> >::vector(std::vector<double>&, unsigned int&)’
/usr/include/c++/4.5/bits/stl_vector.h:241:7: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc> = std::vector<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:227:7: note: std::vector<_Tp, _Alloc>::vector(std::vector::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector::size_type = unsigned int, value_type = std::vector<double>, allocator_type = std::allocator<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:215:7: note: std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, allocator_type = std::allocator<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:207:7: note: std::vector<_Tp, _Alloc>::vector() [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >]
我该如何正确声明Xtrans?
XTrans
,我应该将其声明为二维向量并不传递任何参数吗?由于我想要转置X
,所以我是否只需在X
上编写一个迭代器并将值插入到XTrans
中即可。 - user1155299XTrans
- 将其声明为一个空的二维向量 - 即大小为ctr
的std :: vector <std :: vector <double>>
- 就像你为X
所做的方式一样。然后遍历X
。但请记住它是二维的,因此您有一个外部迭代器,它将迭代ctr
个成员 - 但您的成员是std :: vector <double>
,(当前为空 - 您刚刚声明了ctr
个空的std :: vector <double>
),因此您将为每个ctr
个std :: vector <double>
拥有一个内部迭代器。 - BeeBandX
以填充XTrans
之前,您需要先用某些内容填充X
(或者也许不需要...如果使用迭代器,算法将起作用,只是它什么也不会做 :-)) - BeeBand