【发布时间】:2016-09-26 10:18:23
【问题描述】:
我正在尝试制作一个模板函数来调整嵌套向量的所有维度。
很像这样:resizing multidimensional vector,但对于任意 nr。暗淡。
(我想)该函数将(至少)接受对向量(或vector<vector<T>> 或v<v<v<T>>> 等)的引用和具有所需大小的向量。我现在在尺寸向量中也有 in 索引,但可能不需要它。
到目前为止,这是我最终的结果(可能完全错误):
template<typename V> void resize(vector<V> & V1, vector<int32_t> t, int32_t index) {
int32_t current_size=t.at(index);
cout << "resize dim [" << index << "] to size " << current_size <<endl ;
++index;
if (index < t.size()) {
// for each element ??
// for( int i = 0 ; i < V1.size(); i++ ) { resize (V1.at(i), t, index); } // doesn't work
// for( auto const& e : V1 ) { resize (e, t, index); } // doesn't work
// resize( V1, t, index); // recursive call, works, but doesn't do anything
}
我想避免复制 V1 或其任何内容。我不确定是否有一种方法可以使用迭代器或 for 循环来传递引用。如果没有,可能需要第四个输入来保持 V1 的索引?
顺便说一句,我故意跳过第一个暗淡,它已经是正确的大小。
任何帮助表示赞赏。
【问题讨论】:
-
什么是向量中的“所有维度”?
-
Hayt:每个嵌套向量的一维。所以vector
,一个dim,vector >两个dim,等等。 -
@Wiebe 您可以发布您的代码作为替代答案(我认为在 SO 的问题中发布答案不是一种好方法)
-
@W.F.好的,会的。