在C/C ++中将矩阵读取为2D数组 [英] Read matrix to 2D array in C/C++

查看:53
本文介绍了在C/C ++中将矩阵读取为2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中将数字矩阵读/输入到数组中的最简单方法是什么?

What is the simplest way to read/input a matrix of numbers into an array in C++?

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

This is the file content (dimensions are unknown):

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

我尝试了很多建议的代码,但是似乎都不适合我... :(我想对矩阵执行以下操作:

I've tried a lot of suggested codes, but non of them seem to work for me... :( I want to do the following with the matrix:

MATRIX[i][j] = MATRIX[i][j] + rand()-RAND_MAX/2;

要读取矩阵的if循环中要包含什么?

What to include in the if loop to read the matrix??

#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;
}

推荐答案

首先,如其他建议所述,使用 std :: vector< std :: vector< int>> .它将使事情变得简单得多.

First, as others suggested, use a std::vector<std::vector<int>>. It will make things a lot simpler.

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

所以我们的类型是 IntVector2D .(我定义了一维向量,稍后将使用)

So our type is IntVector2D. (I defined the one-dimensional vector to be used later)

接下来,我们要建立一个循环,一次读取一行,解析该行,并将在该行中找到的int值存储在矩阵的一行中.为此,我们可以将行读取为字符串,然后使用 istringstream 进行解析.

Next, we want to set up a loop that reads one line at a time, parses the line, and stores the int's found on the line in one row of the matrix. To do that, we can read the line into a string, and use istringstream to do the parsing.

最后,对于存储的每一行,我们都会根据您的随机数函数将更改应用于行中的每个项目.

Last, for each line stored, we apply the changes to each item on the row according to your random number function.

因此,以下是所有这些示例的汇总:

So here is a sample of all of this put together:

#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>

typedef std::vector<int> IntVector;
typedef std::vector<IntVector> IntVector2D;

using namespace std;

// random number function to apply
int ApplyRand(int num)
{ return num + rand() - RAND_MAX/2; }

void OutputMatrix(const IntVector2D& m)
{
    cout << "\n";
    IntVector2D::const_iterator it = m.begin();
    while (it != m.end())
    {
        copy(it->begin(), it->end(), ostream_iterator<int>(cout, " "));
        cout << "\n";
        ++it;
    }
}

// Transform the numbers in the matrix
void TransformMatrix(IntVector2D& m)
{
    IntVector2D::iterator it = m.begin();
    while (it != m.end())
    {
        transform(it->begin(), it->end(), it->begin(), ApplyRand);
        ++it;
    }
}

int main()
{
    IntVector2D matrix;
    ifstream pFile("test.txt");
    string s;

    while ( std::getline(pFile, s) )
    {
        // create empty row on back of matrix
        matrix.push_back(IntVector());
        IntVector& vBack = matrix.back();

        // create an istringstream to parse
        istringstream ss(s);

        // parse the data, adding each number to the last row of the matrix
        copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(vBack));
    }
    // output the matrix
    OutputMatrix(matrix);

    // Apply rand to each number
    TransformMatrix(matrix);

    // output the updated matrix 
    OutputMatrix(matrix);
}

输出:

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_inserter 中提取项目,后者负责调用 push_back .

Note the use of std::copy to extract the items from the istringstream, and back_inserter, which is responsible for calling push_back on the last row of the matrix.

此外,使用 std :: transform 允许我们在每个元素上调用一个函数,使用 ApplyRand 作为执行此转换的功能.

Also, the usage of std::transform allows us to call a function on each element, "transforming" the element from the original value to the changed value using ApplyRand as the function to do this transformation.

这篇关于在C/C ++中将矩阵读取为2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆