本文介绍了程序检查2D数组中是否存在任何数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何检查数组中是否存在数字,但 2D数组中不存在。

I know how to check if number exist in the array, but not in a 2D array.

请以二维帮助我。

#include<iostream>
using namespace std;

int main()
{
   int a[3] = { 4,5,6 };
   int b, c;
   int x = 1, fact = 1;

   cout << "enter no ";
   cin >> b;

   for (int i = 0; i < 3; i++)
   {
      if (b == a[i]) {
         c = a[i];
         break;
      }
   }
   cout << "no entered is present" << endl;
}


推荐答案

就像您对一维数组所做的一样,而不是一个,您现在需要遍历行和列。换句话说,您需要再进行一次迭代。

It is like you did for the one-dimensional array, instead of one, you need to now iterate through the rows and column. In another word, you need one more iteration.

#include<iostream>

int main()
{
   int a[2][3]{ { 1,2,3 }, { 4,5,6 } };
   int userInput = 5;
   bool found = false;
   for (int row = 0; !found && row < 2; ++row) // if not found and row size < 2
   {
      for (int col = 0; col < 3; ++col)  // if column size < 3
      {
         if (userInput == a[row][col])   // access the element like this
         {
            // other codes
            std::cout << "No entered is present\n";
            found = true;
            break;
         }
      }
   }
}




但是,我不建议这样使用行大小和列大小。您应该使用更好的(如果您在编译时知道大小),或者(如果在运行时知道大小)。


However, using the row size and column size like this, I will not recommend so. You should be using better std::array(if you know the size at compile time), or std::vector(if the sizes are known at run time).

例如,使用 std :: array ,您可以使用以下代码(示例代码)。使用 ,以及一个简单的函数使代码更具可读性且不易出错。另外,您需要知道编译时已知的大小。 ()

For example, using std::array you could have the following code(example code). Using the range based for-loop, and a simple function makes the code more readable and less error-prone. Also, you need to know the sizes known at compile time. (See live demo)

#include <iostream>
#include <array> // std::array

bool isIn2DArray(const std::array<std::array<int, 3>, 2>& arr, int val) /* noexcept */
{
   // range based for-loop instead of index based looping
   for (const std::array<int, 3> & row : arr)
      for (const int element : row)
         if (element == val)
            return true;    // if found in the array, just return the boolean!
   return false;  // if not found!
}

int main()
{
   std::array<std::array<int, 3>, 2> a{ { { 1,2,3 }, { 4,5,6 } }  };
   int userInput = 5;
   if (isIn2DArray(a, userInput))  // you call the function like this!
   {
      std::cout << "Found in the array!\n";
   }
   else
   {
      std::cout << "Didn't find!\n";
   }
}




如果想知道,如何为任意数组提供 isIn2DArray ,方法是提供大小为,如下所示。 ()


In case of wondering, how to provide isIn2DArray for any arbitrary array, do it by providing the sizes as non-template parameters as below. (See live demo)

#include <array> // std::array

template<std::size_t Row, std::size_t Col>
bool isIn2DArray(const std::array<std::array<int, Col>, Row>& arr, int val)/* noexcept */
{
   // range based for-loop instead of index based looping
   for (const std::array<int, 3> & row : arr)
      for (const int element : row)
         if (element == val)
            return true;    // if found in the array, just return the boolean!

   return false;  // if not found!
}

这篇关于程序检查2D数组中是否存在任何数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:50