使用多维数组的复制
我只是想更多地了解 stl 语义,并在适当的情况下将旧循环转换为算法。 我无法找出将此循环转换为复制调用的最佳方法。 有任何想法吗?
vector< vector<float> > rvec;
const float * r[Max] = ...;
// ...
for (int ri=0; ri<N; ri++)
for (int rj=0; rj<M; rj++)
rvec[ri][rj] = r[ri][rj];
I'm just trying to get more into stl semantics, and converting old loops over to algorithms where appropriate. I'm having trouble figuring out the best way to transform this loop into a call to copy. Any ideas?
vector< vector<float> > rvec;
const float * r[Max] = ...;
// ...
for (int ri=0; ri<N; ri++)
for (int rj=0; rj<M; rj++)
rvec[ri][rj] = r[ri][rj];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 rvec 本身和 rvec 中的每个向量已经具有正确的大小,则不需要调整大小。
If
rvec
itself and each vector inrvec
already has the correct size, then resizing isn't needed.不确定您是否可以仅使用标准算法而不使用函子来做到这一点(并且使用这些算法,代码必然会比上面显着增长)。
你知道,有时简单的循环就是最好的。 STL 的算法非常好,但由于 C++ 还没有匿名函数或内置 lambda,因此采用上面显示的完全可读的代码并将其转换为 STL 算法更多的是一种智力练习,而不是实际的改进,
Not sure you can do this with only the standard algorithms and no functors (and with those, the code is bound to grow significantly than above).
You know, sometimes a plain loop is just best. STL's algorithms are very nice, but since C++ doesn't have anonymous functions or built in lambdas (yet), taking perfectly readable code such as you show above and converting it to STL algorithms is more of an intellectual exercise than actual improvement,
在这种情况下,保留代码原样也不错。 不过,如果您多次编写它,将其抽象为一个单独的函数将是一个好主意。 需要考虑的另一点是,任何不保留或调整大小的解决方案都会浪费时间复制不需要的地方。
In this case just leaving the code as is isn't so bad. Though if you wrote it multiple times abstracting it into a separate function would be a good idea. Another point to consider, any solution that doesn't reserve or resize will waste time copying where you don't need to.
这是我用迭代器快速制作的一个示例......
Here is an example I made a bit rapidly with iterators...