Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将矩阵读取到C/C+中的二维数组+; < > C++中的一个数组中的数字矩阵的读取/输入最简单的方法是什么?< /P>_C++_Arrays_Input_Matrix - Fatal编程技术网

将矩阵读取到C/C+中的二维数组+; < > C++中的一个数组中的数字矩阵的读取/输入最简单的方法是什么?< /P>

将矩阵读取到C/C+中的二维数组+; < > C++中的一个数组中的数字矩阵的读取/输入最简单的方法是什么?< /P>,c++,arrays,input,matrix,C++,Arrays,Input,Matrix,这是文件内容(尺寸未知): 我尝试了很多建议的代码,但似乎没有一个适合我…:( 我想对矩阵执行以下操作: MATRIX[i][j] = MATRIX[i][j] + rand()-RAND_MAX/2; 在if循环中包含什么来读取矩阵 #include <iostream> #include <fstream> ifstream pFile; pFile.open("test.txt"); if (pFile.is_open()) { // SOMETHIN

这是文件内容(尺寸未知):

我尝试了很多建议的代码,但似乎没有一个适合我…:( 我想对矩阵执行以下操作:

MATRIX[i][j] = MATRIX[i][j] + rand()-RAND_MAX/2;
在if循环中包含什么来读取矩阵

#include <iostream>
#include <fstream>

ifstream pFile;
pFile.open("test.txt");

if (pFile.is_open())
{
    // SOMETHING HERE!!!!!??
}
else
{
    printf("Error reading the file!\n");
    return 1;
}
#包括
#包括
ifpfile;
pFile.open(“test.txt”);
if(pFile.is_open())
{
//这里有东西!!!!!??
}
其他的
{
printf(“读取文件时出错!\n”);
返回1;
}

这里有一种使用向量读取未知大小矩阵的简单方法。如果您不知道正在使用的维度,那么向量优于数组的优点是,如果空间不足,您无需担心调整数据结构的大小

    std::vector<std::vector<int> > matrix;
    std::string line;
    int value;

    // read in matrix
    std::ifstream file("path/to/file.txt");
    while(std::getline(file, line)) {
            std::vector<int> row;
            std::istringstream iss(line);
            while(iss >> value){
                    row.push_back(value);
            }
            matrix.push_back(row);
    }
std::向量矩阵;
std::字符串行;
int值;
//读入矩阵
std::ifstream文件(“path/to/file.txt”);
while(std::getline(文件,行)){
std::向量行;
标准::istringstream iss(线);
而(iss>>值){
行。推回(值);
}
矩阵。推回(世界其他地区);
}

首先,正如其他人建议的那样,使用
std::vector
。这将使事情变得简单得多

#include <vector>
typedef std::vector<int> IntVector;
typedef std::vector<IntVector> IntVector2D;
输出:

283 278 284 290 290 286 273 266 266 266 261 252 246
382 380 379 381 382 379 384 387 385 382 376 365 357
285 282 281 279 276 273 272 264 255 255 247 243 237
196 190 186 183 183 180 179 186 191 195 195 188 187
245 237 226 220 221 222 225 228 234 245 252 264 272
283 278 284 290 290 286 273 266 266 266 261 252 246

-16059 2362 -9765 10407 3076 -373 -4632 13241 10845 8347 -10417 12014 7144826 
-6042 -15513 -13007 -4059 -11177 -10563 16395 -1394 -12099 -15854 -15726 -3644
1323 2615 3616 3791 -10660 5616 -1340 -4581 -14259 3784 9531 10159 889
-6293 12510 7614 15122 14133 1470 -11540 -1056 -8481 12065 -9320 9352 11448
16524 16611 3880 -3304 -7439 -6420 11371 -15377 -3833 -13103 6059 -14277 -15823
14006 -7065 -7157 3171 6555 11349 7695 -227 -9388 8253 -772 -1125 14964
注意使用
std::copy
istringstream
back\u inserter
提取项目,后者负责调用矩阵最后一行的
push\u back


另外,使用
std::transform
可以对每个元素调用一个函数,使用
ApplyRand
作为函数将元素从原始值“转换”为更改后的值。

首先,我们声明一个向量向量来保持矩阵不变。 请注意,如果您不知道正在使用的维度,那么向量相对于数组的优势在于,如果空间不足,您无需担心数据结构的大小。这就是为什么我们使用向量而不是数组。 之后,我们使用stringstream从输入读取所有整数。 在while循环中,我们继续执行,直到仍然存在另一行(如果没有更多行,则getline()返回true)。在每个步骤中,我们从输入中读取一行(无论它有多长,我们完全读取它),然后分离该行的整数,并使用字符串流将其放入向量中。然后,我们将该向量添加到matrxi 2D向量中。 我写了这段代码:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;

int main () {

    fstream cin;
    cin.open("input.txt");

    string s;
    vector <vector <int> > matrix;

        while (getline(cin, s)) {

        stringstream input(s);

        int temp;
        vector <int> currentLine;
        while (input >> temp)
            currentLine.push_back(temp);

        matrix.push_back(currentLine);

        }

        for (unsigned int i = 0; i < matrix.size(); i++) {
            for (unsigned int j = 0; j < matrix[i].size(); j++)
                cout << matrix[i][j] << " ";
            cout << endl;
        }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
流式细胞术;
cin.open(“input.txt”);
字符串s;
向量矩阵;
while(getline(cin,s)){
字符串流输入;
内部温度;
矢量电流线;
同时(输入>>温度)
电流线推回(温度);
矩阵。推回(电流线);
}
for(无符号整数i=0;i不能使用
std::vector
。您将为自己省去很多麻烦。到目前为止您尝试了什么?关于如何读取文件,这里已经有数百个线程。谢谢,这太完美了!(:请你解释一下。欢迎@Vito查看我的更新。如果有什么你不明白的,请告诉我。如果对你有用,请接受我的回答。)
283 278 284 290 290 286 273 266 266 266 261 252 246
382 380 379 381 382 379 384 387 385 382 376 365 357
285 282 281 279 276 273 272 264 255 255 247 243 237
196 190 186 183 183 180 179 186 191 195 195 188 187
245 237 226 220 221 222 225 228 234 245 252 264 272
283 278 284 290 290 286 273 266 266 266 261 252 246

-16059 2362 -9765 10407 3076 -373 -4632 13241 10845 8347 -10417 12014 7144826 
-6042 -15513 -13007 -4059 -11177 -10563 16395 -1394 -12099 -15854 -15726 -3644
1323 2615 3616 3791 -10660 5616 -1340 -4581 -14259 3784 9531 10159 889
-6293 12510 7614 15122 14133 1470 -11540 -1056 -8481 12065 -9320 9352 11448
16524 16611 3880 -3304 -7439 -6420 11371 -15377 -3833 -13103 6059 -14277 -15823
14006 -7065 -7157 3171 6555 11349 7695 -227 -9388 8253 -772 -1125 14964
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;

int main () {

    fstream cin;
    cin.open("input.txt");

    string s;
    vector <vector <int> > matrix;

        while (getline(cin, s)) {

        stringstream input(s);

        int temp;
        vector <int> currentLine;
        while (input >> temp)
            currentLine.push_back(temp);

        matrix.push_back(currentLine);

        }

        for (unsigned int i = 0; i < matrix.size(); i++) {
            for (unsigned int j = 0; j < matrix[i].size(); j++)
                cout << matrix[i][j] << " ";
            cout << endl;
        }

    return 0;
}