未显示输出

Showing no output

本文关键字:输出 显示      更新时间:2023-10-16

所以我试图计算堆栈中负数的总数,并编写了这段代码,但它出现了问题,并且没有显示任何输出。我是一个c++的初学者,所以我相信这是完全错误的。

#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
    int count=0;
    while(s.size() !=0){
        if(s.top()<0){
            count++;
            s.pop();
        }       else if(s.top()>0){
            s.pop();
        }       else{}
        cout<<count<<endl;
    }
    return count;
}
int main(){
    stack<int> s;
    s.push(-1);
    s.push(2);
    s.push(-2);
    size_t i(stack<int> s);
    return 0;
}

main()函数中,如果不调用i(),只需重新声明即可。

#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
    int count=0;
    while(s.size() !=0){
        if(s.top()<0){
            count++;
            s.pop();
        }       else if(s.top()>0){
            s.pop();
        }       else{}
        cout<<count<<endl;
    }
    return count;
}
int main(){
    stack<int> s;
    s.push(-1);
    s.push(2);
    s.push(-2);
    size_t i(stack<int> s); // this DOES NOT call the function
    i(s); // <== THIS calls the function!!!
    return 0;
}

您的语句size_t i(stack<int> s);不调用该函数,它只是"告诉"编译器它接受什么参数以及它的返回类型