简体   繁体   English

C ++在2D向量中搜索围绕选定元素的元素?

[英]C++ Search a 2D vector for elements surrounding a chosen element?

Stuck here in my assignment. 卡在我的作业中。 I am working with 2D Vectors. 我正在使用2D向量。 What my professor wants us to do is write a program that has the user enter a size of a matrix (NXN) and print the matrix with random 1's and 0's, which I have done. 我的教授希望我们做的是编写一个程序,让用户输入一个矩阵(NXN)的大小,并用随机的1和0打印该矩阵,这已经完成了。

What I am stuck is that he wants to find "nonzero" elements around a certain element. 我被困的是他想在某个元素周围找到“非零”元素。 For instance: 例如:

0 0 0 0 0 0
0 1 1 0 1 1
1 1 1 1 1 1

Now the user is asked to type in a row and column to (to locate an element) then search for nonzero values adjacent to that element. 现在,要求用户键入行和列以(定位元素),然后搜索与该元素相邻的非零值。 So if rows and columns start at 0, row 1 and column 1 holds the value "1" (the center of the matrix) and has 4 adjacent nonzero elements. 因此,如果行和列从0开始,则行1和列1的值将为“ 1”(矩阵的中心),并具有4个相邻的非零元素。 I am not quite sure where to go from here. 我不太确定从这里出发。 Would I use the find code? 我会使用查找代码吗? I am not sure how to limit that to the adjacent locations of one element. 我不确定如何将其限制为一个元素的相邻位置。

Thank you 谢谢

Hint : if you want to look at the adjacent elements, you can just shift each index by one position. 提示 :如果要查看相邻的元素,可以将每个索引移动一个位置。 For example, if the given (row, column) is (1, 1), the adjacent positions are (0, 1), (2, 1), (1, 0), (1, 2). 例如,如果给定的(行,列)为(1,1),则相邻位置为(0,1),(2,1),(1、0),(1、2)。 You should make sure your code only reads indices in the range (0..N, 0..N). 您应该确保您的代码只读取(0..N,0..N)范围内的索引。

This is your assignment and you should do your best to finish it. 这是您的任务,应尽最大努力完成它。 Go for it and make us proud! 加油吧,让我们感到骄傲!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM