如何迭代2D数组并将每对传递给函数? [英] How to iterate over 2D array and pass each pair to a function?

查看:74
本文介绍了如何迭代2D数组并将每对传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存在.txt文件中的矩阵,它有值,我创建了一个以行和数字为参数的函数,然后返回从matrix.txt文件中获取的值。

我有一个包含成对行/列的二维数组,它们也没有排序它们是随机对,我怎么能迭代这个二维数组并将每一对传递给函数getList(rw,cl)来获得这样的文件结果。



result.txt

行,列,其值

row,columm,其值< br $>




感谢您的帮助,请注意我还是初学者,所以没有预付款提示



我尝试过:



I have a matrix saved in a .txt file, it has values, I created a function that takes row and number as parameters, then it returns the value obtained from the matrix.txt file.
I have a 2D array containing pairs of rows/columns, they are not ordered also they are random pairs, How could I iterate overe this 2D array and pass each pair to the function getList(rw,cl) to obtain such a file as result.

result.txt
row,column,its value
row,columm,its value
etc.

Thanks for any help, notice please I am still beginner, so no advances tips

What I have tried:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const int col_nbr = 7;
const int row_nbr = 3;

int static getValueOf[col_nbr][row_nbr];      
    
void loadMatrix(){

int x, y;


ifstream in("/storage/emulated/0/matrix.txt");

//This my matrix .txt file saved in pc    
/*        
11 12 13 14 15 16 17
21 22 23 24 25 26 27
31 32 33 34 35 36 37
*/      
        
        
        

if (!in){
    cout << "Cannot open file.\n";
    return;
}

  for (x = 0; x < row_nbr; x++){       
     for (y = 0; y < col_nbr; y++){
            
       in >> getValueOf[x][y];
       }
        }

in.close();
}

int getList(int rw,int cl){

return getValueOf[rw-1][cl-1];
}


int main(){

loadMatrix();

//I would take each pair row/columm from this 2D array coords, then I pass the row and the columm to the function getList(rw,cl) to get a list of the coorespondent values from the matrix.txt

int coords [4][2] = {{3,0},{2,1},{6,2},{1,1}};
//int coords has 4 rows and 2 columms
  

    cout<<getList(1,1)<<endl;  //11
    cout<<getList(3,7)<<endl;  //37
    
    
   

return 0;
}

推荐答案

如何放弃全局变量并使用现代 C ++ 功能和库?

(使用输入重定向将数据文件作为 stdin 传递)。

What about abandoning global variables and using modern C++ features and library?
(pass your data file as stdin, using input redirection).
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <array>
using namespace std;

using Matrix = vector < vector <int > >;

int main()
{
  Matrix m;
  string line;
  while ( getline( cin, line) )
  {
    vector <int> row;
    istringstream iss(line);
    while ( iss )
    {
      int i;
      iss >> i;
      row.push_back(i);
    }
    m.push_back( row );
  }

  array< array <int, 2>, 4> arr{{{3,0},{2,1},{6,2},{1,1}}};

  for ( const auto & a : arr)
  {
    int r = a[1]; // it looks there is some confusion on 'rows' and 'columns' interpretation...
    int c = a[0];
    cout << "M[" << r << "," << c << "]= " << m[r][c] << "\n";
  }
}


这篇关于如何迭代2D数组并将每对传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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