为什么我的 VS 代码终端不是 运行 代码?

Why is my VS code terminal not running the code?

终端工作正常,而 运行 我以前的代码除了最近的代码。很可能我的代码中存在一些错误,但我在在线编译器中尝试了 运行 相同的方法并且它工作得很好。

终端截图如下:

这是我的代码:

#include<bits/stdc++.h>
using namespace std;

void stair_search(int a[][1000], int row, int col, int k){
    int i=0, j=col-1;
    while(i < row and j >= 0){
        if(a[i][j] == k){
            cout<<k<<" found at "<<i<<", "<<j<<endl;
            return;
        }

        if(a[i][j] > k){
            j--;
        }else{
            i++;
        }
    }

    cout<<"Element not present!"<<endl;
}

int main(){

    int a[1000][1000];
    int row, col;
    cin>>row>>col;

    // Take input
    for (int i=0; i<row; i++){
        for (int j=0; j<col; j++){
            cin>>a[i][j];
        }
    }

    stair_search(a,row,col,16);

    return 0;
}

尝试将 try/catch 块添加到您的函数中,看看是否出现错误。像这样:

void stair_search(int a[][1000], int row, int col, int k){
try{
int i=0, j=col-1;
while(i < row and j >= 0){
    if(a[i][j] == k){
        std::cout<<k<<" found at "<<i<<", "<<j<<"\n";
        return;
    }

    if(a[i][j] > k){
        j--;
    }else{
        i++;
    }
}

std::cout<<"Element not present!"<<"\n";
}
 catch(const std::exception& ex){
    std::cout << "Error in stair_search: " << ex.what() << "\n";
    return -1;
 }
}

int main(){

int a[1000][1000];
int row, col;
std::cin>>row>>col;

// Take input
for (int i=0; i<row; i++){
    for (int j=0; j<col; j++){
        std::cin>>a[i][j];
    }
}

stair_search(a,row,col,16);

return 0;    
}

还有“使用命名空间标准;”被认为是非常糟糕的做法,因为您可以重叠同名函数。不要这样做。 并且应该避免 std::endl,它会在每次调用时刷新 I/O 流缓冲区,这非常慢。

它似乎对我有用,但也许你的编译器不同...是

 void stair_search(int a[][1000], int row, int col, int k)

被认为是未定义的行为,不应该是:

 void stair_search(int a[1000][1000], int row, int col, int k)

或:

 template<size_t sA, size_t sB>
 void stair_search(std::array<std::array<int,sB>sA> a, int row, int col, int k)

尝试打开警告。