假设有以下两个二维数组:

#include <iostream>
#include <vector>
#include <ranges>

int main()
{
  std::vector<std::vector<int>> matrix1{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  std::vector<std::vector<int>> matrix2{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}};
  
  // 使用 std::views::join 连接两个二维数组
  auto joined_matrix = matrix1 | std::views::join(matrix2);
  
  // 输出结果
  for (const auto& row : joined_matrix) {
    for (const auto& element : row) {
      std::cout << element << " ";
    }
    std::cout << std::endl;
  }
}

输出结果为:

1 2 3 10 11 12 
4 5 6 13 14 15 
7 8 9 16 17 18

可以看到,使用 std::views::join 连接两个二维数组,生成了一个包含所有元素的一维数组。

举个C++ 例子,使用std::views::join

原文地址: https://www.cveoy.top/t/topic/vSw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录