程序检查2D数组中是否存在任何数字

编程入门 行业动态 更新时间:2024-10-27 06:21:15
本文介绍了程序检查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; }

推荐答案

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

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

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

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


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


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数组中是否存在任何数字

本文发布于:2023-11-27 15:45:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   组中   数字   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!