企業(yè)加盟網(wǎng)站建設(shè)北京seo營銷培訓(xùn)
在 Qt 中,可以通過 QJsonDocument
和 QJsonArray
方便地存取 JSON 格式的矩陣數(shù)據(jù)。以下是存儲(chǔ)和讀取矩陣數(shù)據(jù)的完整實(shí)現(xiàn)示例。
1. 矩陣存儲(chǔ)為 JSON
將矩陣(QVector<QVector<double>>
或其他二維數(shù)組)存儲(chǔ)為 JSON 文件。
實(shí)現(xiàn)代碼
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QVector>
#include <QDebug>// 將矩陣保存為 JSON 格式
void saveMatrixToJson(const QVector<QVector<double>>& matrix, const QString& filePath) {QJsonArray jsonMatrix;// 轉(zhuǎn)換矩陣為 QJsonArrayfor (const auto& row : matrix) {QJsonArray jsonRow;for (double value : row) {jsonRow.append(value);}js