C++笔记之初始化二维矩阵的方法

C++笔记之初始化二维矩阵的方法

------ 2023年5月20日 上海

code review!

文章目录

  • C++笔记之初始化二维矩阵的方法
    • 一.常见方法
      • [1. 使用数组](#1. 使用数组)
      • [2. 使用向量](#2. 使用向量)
      • [3. 使用数组的动态分配](#3. 使用数组的动态分配)
      • [4. 使用嵌套的 std::vector 并使用resize方法](#4. 使用嵌套的 std::vector 并使用resize方法)
      • [5. 初始化固定大小的 std::array](#5. 初始化固定大小的 std::array)
    • 二.C++中使用vector初始化二维矩阵的所有方法
      • [1. 列表初始化](#1. 列表初始化)
      • [2. 使用 `resize` 方法------重要:](#2. 使用 resize 方法——重要:)
      • [3. 使用 `push_back` 方法:](#3. 使用 push_back 方法:)
      • [4. 使用 `emplace_back` 方法:](#4. 使用 emplace_back 方法:)
      • [5. 使用 `assign` 方法------重要:](#5. 使用 assign 方法——重要:)
      • [6. 使用 `insert` 方法:](#6. 使用 insert 方法:)
    • 三.根据一个已有的二维矩阵使用std::vector来初始化一个新的二维矩阵
      • [1. 使用拷贝构造函数------重要](#1. 使用拷贝构造函数——重要)
      • [2. 使用赋值运算符------重要](#2. 使用赋值运算符——重要)
      • [3. 使用 `assign` 方法](#3. 使用 assign 方法)
      • [4. 使用 `std::copy` 函数](#4. 使用 std::copy 函数)
      • [5. 使用 `std::copy_n` 函数](#5. 使用 std::copy_n 函数)
      • [6. 使用范围基础的 for 循环和 `push_back` 方法](#6. 使用范围基础的 for 循环和 push_back 方法)
    • 三.根据一个已有的二维矩阵使用std::vector来初始化一个新的同维度的,所有元素都为0的二维矩阵
      • [1. 使用 `resize` 方法和循环------重要](#1. 使用 resize 方法和循环——重要)
      • [2. 使用 `resize` 方法和 `std::transform` 函数](#2. 使用 resize 方法和 std::transform 函数)
      • [3. 使用嵌套的 `resize` 方法](#3. 使用嵌套的 resize 方法)

一.常见方法

1. 使用数组

你可以使用二维数组来初始化一个矩阵:

cpp 复制代码
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

2. 使用向量

你也可以使用std::vector来创建和初始化二维矩阵:

cpp 复制代码
std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

3. 使用数组的动态分配

如果你需要动态地创建矩阵(例如,其尺寸在运行时确定),可以使用new操作符:

cpp 复制代码
int rows = 3;
int cols = 3;
int** matrix = new int*[rows];
for(int i = 0; i < rows; ++i)
    matrix[i] = new int[cols];

然后你可以使用循环来初始化这个矩阵。

4. 使用嵌套的 std::vector 并使用resize方法

如果你需要动态地创建和初始化矩阵,但你更喜欢使用std::vector,你可以这样做:

cpp 复制代码
int rows = 3;
int cols = 3;
std::vector<std::vector<int>> matrix;
matrix.resize(rows, std::vector<int>(cols, 0)); // 这将创建一个3x3的矩阵,并用0填充

5. 初始化固定大小的 std::array

对于固定大小的二维矩阵,可以使用std::array

cpp 复制代码
std::array<std::array<int, 3>, 3> matrix = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } };

二.C++中使用vector初始化二维矩阵的所有方法

在 C++ 中,使用 std::vector 来初始化二维矩阵是常见的做法,因为它提供了灵活的大小和方便的内存管理。以下是一些方法:

1. 列表初始化

你可以在声明时就初始化 std::vector

cpp 复制代码
std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

2. 使用 resize 方法------重要:

如果你已经知道矩阵的大小,但要稍后填充值,可以使用 resize 方法:

cpp 复制代码
std::vector<std::vector<int>> matrix;
matrix.resize(rows, std::vector<int>(cols));

3. 使用 push_back 方法:

你也可以创建一个空的 std::vector,然后使用 push_back 方法来添加行:

cpp 复制代码
std::vector<std::vector<int>> matrix;
for(int i = 0; i < rows; ++i) {
    std::vector<int> row;
    for(int j = 0; j < cols; ++j) {
        row.push_back(value);
    }
    matrix.push_back(row);
}

4. 使用 emplace_back 方法:

类似于 push_back,但 emplace_back 直接在原地构造新元素,通常效率更高:

cpp 复制代码
std::vector<std::vector<int>> matrix;
for(int i = 0; i < rows; ++i) {
    matrix.emplace_back(std::vector<int>(cols, value));
}

5. 使用 assign 方法------重要:

assign 方法可以用来替换 std::vector 中的所有元素:

cpp 复制代码
std::vector<std::vector<int>> matrix;
matrix.assign(rows, std::vector<int>(cols, value));

6. 使用 insert 方法:

insert 方法可以在 std::vector 的任何位置插入新元素:

cpp 复制代码
std::vector<std::vector<int>> matrix;
for(int i = 0; i < rows; ++i) {
    matrix.insert(matrix.end(), std::vector<int>(cols, value));
}

注意,在这些例子中,rowscolsvalue 都是整数变量,分别表示矩阵的行数、列数和初始值。

三.根据一个已有的二维矩阵使用std::vector来初始化一个新的二维矩阵

在C++中,如果你已经有一个二维矩阵,你可以通过多种方式使用std::vector来初始化一个新的二维矩阵:

1. 使用拷贝构造函数------重要

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix(old_matrix);

2. 使用赋值运算符------重要

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix = old_matrix;

3. 使用 assign 方法

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix;
new_matrix.assign(old_matrix.begin(), old_matrix.end());

4. 使用 std::copy 函数

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix(old_matrix.size());
std::copy(old_matrix.begin(), old_matrix.end(), new_matrix.begin());

5. 使用 std::copy_n 函数

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix(old_matrix.size());
std::copy_n(old_matrix.begin(), old_matrix.size(), new_matrix.begin());

6. 使用范围基础的 for 循环和 push_back 方法

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix;
for (const auto &row : old_matrix) {
    new_matrix.push_back(row);
}

以上就是使用 std::vector 根据已有的二维矩阵初始化新的二维矩阵的一些方法。根据你的具体需求和优化考虑,你可能需要选择不同的方法。

三.根据一个已有的二维矩阵使用std::vector来初始化一个新的同维度的,所有元素都为0的二维矩阵

在C++中,如果你已经有一个二维矩阵,并希望创建一个新的、同维度的、所有元素都为0的二维矩阵,以下是一些方法:

1. 使用 resize 方法和循环------重要

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix;
for(const auto &row : old_matrix) {
    new_matrix.push_back(std::vector<int>(row.size(), 0));
}

2. 使用 resize 方法和 std::transform 函数

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix(old_matrix.size());
std::transform(old_matrix.begin(), old_matrix.end(), new_matrix.begin(), [](const std::vector<int>& row) {
    return std::vector<int>(row.size(), 0);
});

3. 使用嵌套的 resize 方法

cpp 复制代码
std::vector<std::vector<int>> old_matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> new_matrix;
new_matrix.resize(old_matrix.size());
for(size_t i = 0; i < old_matrix.size(); ++i) {
    new_matrix[i].resize(old_matrix[i].size(), 0);
}

在以上的所有例子中,old_matrix 是你已有的二维矩阵,new_matrix 是新创建的二维矩阵。所有的新矩阵元素都被初始化为0,并且新矩阵的尺寸与原矩阵相同。

相关推荐
咩咩大主教6 分钟前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
Ylucius2 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
是店小二呀2 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
洛寒瑜2 小时前
【读书笔记-《30天自制操作系统》-23】Day24
开发语言·汇编·笔记·操作系统·应用程序
ephemerals__2 小时前
【c++】动态内存管理
开发语言·c++
CVer儿2 小时前
条件编译代码记录
开发语言·c++
星迹日3 小时前
C语言:联合和枚举
c语言·开发语言·经验分享·笔记
程序猿练习生3 小时前
C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)
c++·算法·leetcode
汉字萌萌哒3 小时前
【2022 CCF 非专业级别软件能力认证第一轮(CSP-J1)入门级 C++语言试题及解析】
数据结构·c++·算法
th新港3 小时前
CCF201909_1
数据结构·c++·算法·ccf