有没有一种方法可以从输入中找到二维数组的行和列

Is there a way to find row and column of an two dimensional array from input alone?

本文关键字:二维数组 输入 一种 方法 有没有      更新时间:2023-10-16

作为我正在进行的更大任务的一部分,我正试图找到一种方法,仅从输入中找到二维数组的行和列。

目前我所做的设置参数只是要求行和列。(见下文/忽略一些指令,初始化)。

但是我想做的是能够做的就是跳过我通过输入方式设置行和列的部分;相反,我们可以用矩阵的方式输入数组的值然后用这种方式得到行和列。这可能吗?

//what I'd to be able to do
Enter values:
1 2 3 4
5 6 7 8
//the array would then be ex_array[2][4]
//current method I am using
#include<iostream>
#include<cmath>
#include <iomanip> 
using namespace std;
int main()
{
   int n,m;
   double sqt;
   double elevation;
   double answer;
   int array[10][10];
   int lowest,highest;
   int lowest_x_coordinate_index;
   int lowest_y_coordinate_index;
   int highest_x_coordinate_index;
   int highest_y_coordinate_index;
   cout<<"Enter No of rows: ";
   cin>>m;
   cout<<"Enter No of columns: ";
   cin>>n;
   cout<<"Enter values:n";
   //Read array values from keyboard
   for(int i=0;i<m;i++)
   {
       for(int j=0;j<n;j++)
       {
           cin>>array[i][j];
       }
   }
}

不能,除非通过引用传递数组,否则数组将衰变成指向其第一个元素的指针。通过引用传递的例子:

#include <iostream>
template <typename T, size_t M, size_t N>
void f(T (&arr)[M][N])
{
    std::cout << "Size: " << M << " " << N;
}
int main()
{
    int arr[2][3];
    f(arr); // can deduce the size here
}

如果你问是否可以创建一个可变长度的数组,答案仍然是否定的,c++不允许可变大小的数组。您唯一的选择是使用动态分配,

int M = 10, N = 20; // sizes, can be read also from input
int* array = new int*[M];
for(size_t i = 0 ; i < N; ++i)
    array[i] = new int[N];

用完后别忘了删掉

for(size_t i = 0 ; i < N; ++i)
    delete[] array[i];
delete[] array;

正如你所看到的,事情开始看起来不那么好。不要使用普通的c风格数组,尝试使用标准库std::vector(或std::array用于编译时固定大小的数组)。它们更灵活,更不容易出错。例子:

std::vector<std::vector<int>> v(M, std::vector<int>(N)); // vector of vector, M x N

是的(如果我对你的问题理解正确的话)。

可以这样做:

Enter values:
1 2 3 4
5 6 7 8
                         <- empty line
//the array would then be ex_array[2][4]

你必须使用:

  • getline逐行读取输入
  • stringstream解析每一行
  • 一个矢量来构建每一行
  • 用于构建完整数组的数组向量

但是…在c++中没有方便的方法来声明一个运行时已知大小的2D数组!(参考:我如何在c++中使用new声明一个2d数组?),你最多以数组的数组结束。但是如果你接受用静态大小声明数组,就像在你的代码中那样,那就简单多了:

#include <string>
#include <iostream>
#include <sstream>
#include <vector>
int main() {
    std::string line;
    int cols = -1;
    int linenum = 0;
    int arr[10][10];
    for(;;) {
        linenum++;
        int i;
        std::getline(std::cin, line);
        if (line == std::string("")) break;
        if (linenum >= 10) {
            std::cerr << "Too much lines (limited to 10)" << std::endl;
            return 1;
        }
        std::istringstream ss(line);
        std::vector<int> values;
        for(;;) {
            ss >> i;
            if (ss.fail()) break;
            values.push_back(i);
        }
        if (! ss.eof()) {
            std::cerr << "Incorrect input line " << linenum << std::endl;
            return 1;
        }
        if (cols == -1) {
            cols = values.size();
            if (cols > 10) {
                std::cerr << "Too much columns " << cols << ">10" << std::endl;
                return 1;
            }
        }
        else if (cols != values.size()) {
            std::cerr << "Error line " << linenum << " : " << cols;
            std::cerr << " columns expected - found " << values.size();
            std::cerr << std::endl;
            return 1;
        }
        for(int i=0; i<cols; i++) arr[linenum - 1][i] = values[i];
    }
    int nrows = linenum -1;
    std::cout << nrows << " lines - " << cols << " columns" << std::endl;
    for (int i=0; i<nrows; i++) {
        for (int j=0; j<cols; j++) {
            std::cout << " " << arr[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

输入:

1 2 3 4
5 6 7 8

给出:

2 lines - 4 columns
 1 2 3 4
 5 6 7 8