考虑以下代码片段,
#include <iostream>
#include <valarray>
using namespace std;
std::ostream & operator<<(std::ostream & out, const std::valarray<int> inputVector);
typedef std::valarray<std::valarray<int> > val2d;
int main()
{
val2d g(std::valarray<int>(10),4);
for (uint32_t n=0; n<4; ++n){
for (uint32_t m=0; m<10; ++m){
g[n][m] = n*10 + m;
}
}
std::valarray<int> g_slice_rs = g[1][std::slice(0,10,1)]; // row slice
//std::valarray<int> g_slice_cs = g[std::slice(0,1,3)][0]; // column slice (comment out)
cout<<"row slice :: "<<g_slice_rs<<endl;
//cout<<"column slice :: "<<g_slice_cs<<endl; // (comment out)
return 0;
}
std::ostream & operator<<(std::ostream & out, const std::valarray<int> inputVector)
{
uint32_t vecLength = inputVector.size();
out<<"[";
for (uint32_t i=0; i<vecLength; ++i)
{
out <<inputVector[i]<<", ";
}
out<<"]"<<endl;
return out;
}
在这里,我可以访问行切片,但不能访问列切片(如注释中所示)。是否有任何解决方法来访问列切片? 该线程不提供答案。