You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效用小vector填充大vector?替代双层循环方案探讨

高效填充二维vector的优化需求与现有实现

我需要将一个二维vector(src)的内容填充到另一个二维vector(dest)的指定坐标区域,目前的实现依赖嵌套循环,效率偏低。由于该操作每秒需要执行上千次且数据规模更大,希望找到无需显式循环的更优方案,比如利用内存连续特性的地址范围操作,是否有可行的思路?

以下是能实现需求但效率较低的工作代码:

#include <iostream>
#include <vector>
#include <string>

typedef std::vector<std::vector<int>> Vec2i;

void Vec2DPrinter(Vec2i vec)
{
    size_t vec_h = vec.size();

    for (size_t y = 0; y < vec_h; y++)
    {
        size_t vec_w = vec.at(y).size();

        for (size_t x = 0; x < vec_w; x++)
        {
            std::cout << vec.at(y).at(x);
        }

        std::cout << std::endl;
    }
}

void Vec2DFiller(Vec2i &dest, Vec2i src, int dest_x, int dest_y)
{
    int src_h = (int)src.size();

    // Loop to fill dest vector with elements of src vector at specific coords
    for (int y = 0; y < src_h; y++)
    {
        int src_w = (int)src.at(y).size();
        int dest_h = (int)dest.size();

        if ((y + dest_y) >= dest_h)
        {
            break;
        }

        if ((y + dest_y) < 0)
        {
            continue;
        }

        int dest_w = (int)dest.at(y + dest_y).size();

        for (int x = 0; x < src_w; x++)
        {
            if (x + dest_x >= dest_w)
            {
                break;
            }

            if (x + dest_x < 0)
            {
                continue;
            }

            dest.at(y + dest_y).at(x + dest_x) = src.at(y).at(x);
        }
    }
}

int main(void)
{
    // This is just visual example (i know how to fill vectors)
    Vec2i dest =
    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    };

    Vec2i src =
    {
        { 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1 }
    };

    std::cout << "Destination contents (before change):" << std::endl;
    Vec2DPrinter(dest);
    std::cout << "\nSource contents:" << std::endl;
    Vec2DPrinter(src);

    // Destination coords
    int dest_x = 3;
    int dest_y = 4;

    // Key function to fill one vector with another
    Vec2DFiller(dest, src, dest_x, dest_y);

    std::cout << "\nDestination contents (after change):" << std::endl;
    Vec2DPrinter(dest);

    std::string exit = "";
    std::getline(std::cin, exit);

    return 0;
}

核心填充逻辑

当前实现的核心嵌套循环如下:

// Destination coords
int dest_x = 3;
int dest_y = 4;

int src_h = (int)src.size();

// Loop to fill dest vector with elements of src vector at specific coords
for (int y = 0; y < src_h; y++)
{
    int src_w = (int)src.at(y).size();
    int dest_h = (int)dest.size();

    if ((y + dest_y) >= dest_h)
    {
        break;
    }

    if ((y + dest_y) < 0)
    {
        continue;
    }

    int dest_w = (int)dest.at(y + dest_y).size();

    for (int x = 0; x < src_w; x++)
    {
        if (x + dest_x >= dest_w)
        {
            break;
        }

        if (x + dest_x < 0)
        {
            continue;
        }

        dest.at(y + dest_y).at(x + dest_x) = src.at(y).at(x);
    }
}

优化背景与尝试

这是我目前能想到的最优实现(已对原版的if语句做了调整,原版更紧凑),但由于操作频率高、数据规模大,急需优化。我已经尝试过std::fill和std::copy:

  • std::fill不符合需求,它只能填充单一值;
  • std::copy本质还是基于循环实现,没有带来效率提升。

希望找到真正能减少循环开销的方案,比如利用内存连续特性直接操作地址范围。

内容的提问来源于stack exchange,提问作者skrovno_CZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:37:01