提问者:小点点

如何将三维尺寸不固定的三维数组展平成一维数组?


给定一个2D数组,其中每个(x,y)单元包含一个不同大小的字符串向量(为了简单起见)。

将此数据结构扁平化为一维数组的最有效方法是什么,即创建一个函数,将每个字符串注入到{1,。。。,n},其中n是数据结构中字符串的总数。

谢谢你的帮助


共1个答案

匿名用户

简单直接的方法不适合你吗?

#include <vector>
#include <string>

int main() {
        std::vector<std::string> omg[3][4];
        std::vector<std::string> rv;
        for(auto const &row: omg) {
                for(auto const &cell: row) {
                        for(auto const &str: cell) {
                                rv.push_back(str);
                        }
                }
        }
}