C++ 显示没有输出

C++ 显示没有输出,c++,data-structures,stack,C++,Data Structures,Stack,因此,我试图计算堆栈中负数的总数,我编写了这段代码,但它出了问题,没有显示任何输出。我是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){ c

因此,我试图计算堆栈中负数的总数,我编写了这段代码,但它出了问题,没有显示任何输出。我是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;
}

您的账单大小为s;不调用函数,它只是告诉编译器它接受什么参数以及它的返回类型。

可能是因为它甚至没有编译。这甚至是无效的。std::stack没有调用-运算符Try!s、 是的,对不起,我忘了在这里写。太好了。现在,如果你认为大小是s;实际上,主要是执行i函数,你错了。没有。它声明了一个函数。你想要的仅仅是;我没有看到任何创建任何输出的调用。在i函数本身中尝试std::cout@RSahu its。我也花了一分钟看了。