C++查找整数是否存在于矩阵的一列中

C++ Find if integer exits in a column of a matrix

本文关键字:一列 整数 查找 是否 存在 于矩阵 C++      更新时间:2023-10-16

如果我有

int x = 5

M=|3 4 5||5 2 1||5 6 2|

如何检查特定列是否包含x?所以当我检查第二列时,我会得到false。如果我检查第一列或第三列,我就会得到正确的结果。在MATLab,我会写

如果find(M(:,2)==x)>1disp其他的disp('false')终止

检查x是否在第二列。我想知道c++中是否有类似的方法。

感谢

试试看。

bool colContains(const std::vector<std::vector<int> > &matrix, int x, int col) {
    for(int y = 0; y < matrix.size(); y++) {
        if(matrix[y].size() < col) continue; //Make sure we don't go out of bounds
        if(matrix[y][col] == x) return true;
    }
    return false;
}