Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法从堆栈中检索所有内容_C++_Stack - Fatal编程技术网

C++ 无法从堆栈中检索所有内容

C++ 无法从堆栈中检索所有内容,c++,stack,C++,Stack,我很难理解为什么这段代码不能像我期望的那样输出?有什么想法吗 输出:?你是 预期:?你是如何你好 #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<string> sentence; sentence.push("Hello,"); sentence.pus

我很难理解为什么这段代码不能像我期望的那样输出?有什么想法吗

输出:?你是

预期:?你是如何你好

#include <iostream> 
#include <stack> 
#include <string> 
using namespace std; 

 int main() 
 { 
     stack<string> sentence; 

     sentence.push("Hello,"); 
     sentence.push("how"); 
     sentence.push("are"); 
     sentence.push("you"); 
     sentence.push("?"); 

     for (int i=0; i<sentence.size(); ++i) { 
         cout << sentence.top() << " "; 
         sentence.pop(); 
     } 
     cout << endl; 

     return 0; 
 }
#包括
#包括
#包括
使用名称空间std;
int main()
{ 
堆叠句子;
推(“你好,”);
句子。推(“如何”);
句子。推(“are”);
推(“你”);
句子。按(“?”);

对于(int i=0;i,因为调用句子.pop()会将堆栈大小减少1,同时增加i。这样只能得到一半的值

改为这样做:

 while (!sentence.empty()){ 
     cout << sentence.top() << " "; 
     sentence.pop(); 
 } 
while(!句子.empty()){

cout因为调用句子.pop()会将堆栈大小减少1,同时增加i。这样只能得到一半的值

改为这样做:

 while (!sentence.empty()){ 
     cout << sentence.top() << " "; 
     sentence.pop(); 
 } 
while(!句子.empty()){

cout当您在循环中弹出堆栈时,您正在更改堆栈的大小,从而影响循环执行的次数

相反,我会:

while (!sentence.empty())
{
    cout << sentence.top() << " ";
    sentence.pop();
}
while(!句子.empty())
{

cout当您在循环中弹出堆栈时,您正在更改堆栈的大小,从而影响循环执行的次数

相反,我会:

while (!sentence.empty())
{
    cout << sentence.top() << " ";
    sentence.pop();
}
while(!句子.empty())
{
cout当您发出pop()时,堆栈的大小将减小,而变量i将独立于堆栈的大小而增大。我建议将您的循环替换为以下循环

 while  ( !sentence.empty() ) { 
     cout << sentence.top() << " "; 
     sentence.pop(); 
 } 
while(!句子.empty()){
cout当您发出pop()时,堆栈的大小将减小,而变量i将独立于堆栈的大小而增大。我建议将您的循环替换为以下循环

 while  ( !sentence.empty() ) { 
     cout << sentence.top() << " "; 
     sentence.pop(); 
 } 
while(!句子.empty()){

当您从中弹出项目时,无法考虑堆栈的大小。请尝试
while(!sence.empty())
。当您从中弹出项目时,请考虑堆栈的大小。请尝试
while(!sence.empty())
。将“sencee.size()>0”更改为“sencee.size()>0”。或者更好的做法是,“!sence.empty()@traal:你可以写你自己的答案,你知道吗?@traal:你的第一个建议是重言式的,第二个没有用。把'sencee.size()>0'改为'sence.size()>0'。或者更好,'!sence.empty()'。@traal:你可以写你自己的答案,你知道吗?@traal:你的第一个建议是重言式的,第二个没有用。