使用多维数组的复制

发布于 2024-07-26 04:55:09 字数 296 浏览 5 评论 0原文

我只是想更多地了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野心澎湃 2024-08-02 04:55:09
rvec.resize(Max);
for (int i = 0; i < Max; ++i) {
  rvec[i].resize(M);
  std::copy(r[i], r[i] + M, rvec[i].begin());
}

如果 rvec 本身和 rvec 中的每个向量已经具有正确的大小,则不需要调整大小。

rvec.resize(Max);
for (int i = 0; i < Max; ++i) {
  rvec[i].resize(M);
  std::copy(r[i], r[i] + M, rvec[i].begin());
}

If rvec itself and each vector in rvec already has the correct size, then resizing isn't needed.

暮光沉寂 2024-08-02 04:55:09

不确定您是否可以仅使用标准算法而不使用函子来做到这一点(并且使用这些算法,代码必然会比上面显着增长)。

你知道,有时简单的循环就是最好的。 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,

娇纵 2024-08-02 04:55:09

在这种情况下,保留代码原样也不错。 不过,如果您多次编写它,将其抽象为一个单独的函数将是一个好主意。 需要考虑的另一点是,任何不保留或调整大小的解决方案都会浪费时间复制不需要的地方。

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.

沉鱼一梦 2024-08-02 04:55:09

这是我用迭代器快速制作的一个示例......

#include <algorithm>
using std::copy;
using std::for_each;
using std::random_shuffle;

#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::iterator;
using std::vector;

void Write(int i)
{
    cout << i << endl;
}

int main()
{
    const short MAX_LIST = 1000,
                MAX_NUMBER = 100;
    float number = 0;
    vector<vector<float> > v (MAX_LIST),
                           v2 (MAX_LIST);
    vector<float> list(MAX_NUMBER);

    //Fills list and fills v with list
    for(vector<vector<float> >::iterator v_iter = v.begin(); v_iter != v.end(); ++v_iter)
    {
        for(vector<float>::iterator list_iter = list.begin(); list_iter != list.end(); ++list_iter)
        {
            *list_iter = number;
            ++number;
        }
        *v_iter = list;
    }

    //write v to console
    for(vector<vector<float> >::iterator v_iter = v.begin(); v_iter != v.end(); ++v_iter)
    {
        for_each(v_iter->begin(), v_iter->end(), Write);
    }

    //copy v to v2
    cout << "Copying..." << endl;
    copy(v.begin(), v.end(), v2.begin());
    cout << "Finished copying!" << endl;

    cout<< "Shuffling..." << endl;
    random_shuffle(v2.begin(), v2.end());
    cout << "Finished shuffling!" << endl;

    //write v2 to console
    for(vector<vector<float> >::iterator v_iter = v2.begin(); v_iter != v2.end(); ++v_iter)
    {
        for_each(v_iter->begin(), v_iter->end(), Write);
    }
}

Here is an example I made a bit rapidly with iterators...

#include <algorithm>
using std::copy;
using std::for_each;
using std::random_shuffle;

#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::iterator;
using std::vector;

void Write(int i)
{
    cout << i << endl;
}

int main()
{
    const short MAX_LIST = 1000,
                MAX_NUMBER = 100;
    float number = 0;
    vector<vector<float> > v (MAX_LIST),
                           v2 (MAX_LIST);
    vector<float> list(MAX_NUMBER);

    //Fills list and fills v with list
    for(vector<vector<float> >::iterator v_iter = v.begin(); v_iter != v.end(); ++v_iter)
    {
        for(vector<float>::iterator list_iter = list.begin(); list_iter != list.end(); ++list_iter)
        {
            *list_iter = number;
            ++number;
        }
        *v_iter = list;
    }

    //write v to console
    for(vector<vector<float> >::iterator v_iter = v.begin(); v_iter != v.end(); ++v_iter)
    {
        for_each(v_iter->begin(), v_iter->end(), Write);
    }

    //copy v to v2
    cout << "Copying..." << endl;
    copy(v.begin(), v.end(), v2.begin());
    cout << "Finished copying!" << endl;

    cout<< "Shuffling..." << endl;
    random_shuffle(v2.begin(), v2.end());
    cout << "Finished shuffling!" << endl;

    //write v2 to console
    for(vector<vector<float> >::iterator v_iter = v2.begin(); v_iter != v2.end(); ++v_iter)
    {
        for_each(v_iter->begin(), v_iter->end(), Write);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文