造一个float类型二维矩阵,并将二维矩阵存快速储到一个float*中(memcpy)

cpp 复制代码
// 创建并初始化一个二维数组
std::vector<std::vector<float>> createAndInitializeArray(int rows, int cols)
{
    std::vector<std::vector<float>> array(rows, std::vector<float>(cols));
    float value = 0.0f;
    for (int i = 0; i < rows; i++) 
    {
        for (int j = 0; j < cols; j++) 
        {
            array[i][j] = value;
            value += 1.0f;
        }
    }
    return array;
}

// 将二维数组的数据复制到一维数组并返回指针
float* flatten2DArray(std::vector<std::vector<float>>& inputArray)
{
    int rows = inputArray.size();
    int cols = inputArray[0].size();

    float* flattenedArray = new float[rows * cols];
    for (int i = 0; i < rows; i++)
    {
        std::memcpy(flattenedArray + i * cols, inputArray[i].data(), cols * sizeof(float));
    }
    return flattenedArray;
}

int main() {
    int rows = 360;
    int cols = 1000;

    // 造假数据并将二维数组的数据复制到一维数组
    std::vector<std::vector<float>> FalseMatrix = createAndInitializeArray(rows, cols);
    float* fdata = flatten2DArray(FalseMatrix);

    // 使用 fdata

    delete [] fdata; // 当您不再需要该数组时,务必执行释放内存的操作

    return 0;
}
相关推荐
DoraBigHead44 分钟前
小哆啦解题记——两数失踪事件
前端·算法·面试
不太可爱的大白44 分钟前
Mysql分片:一致性哈希算法
数据库·mysql·算法·哈希算法
Tiandaren5 小时前
Selenium 4 教程:自动化 WebDriver 管理与 Cookie 提取 || 用于解决chromedriver版本不匹配问题
selenium·测试工具·算法·自动化
岁忧6 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说8 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy8 小时前
力扣61.旋转链表
算法·leetcode·链表
谭林杰9 小时前
B树和B+树
数据结构·b树
卡卡卡卡罗特10 小时前
每日mysql
数据结构·算法
chao_78910 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找