cin流的状态和缓存刷新问题

来源:互联网 发布:微盘程序源码 编辑:程序博客网 时间:2024/05/25 08:15
#include "stdafx.h"
#include 
<iostream> 
#include 
<list> 
#include 
<vector> 
#include 
<limits>
using namespace std; 

int main(void

    vector 
<int>  ivec; 
    list 
<int>  ilst; 
    
int ival; 
    cout 
<< "the vetor's numbers" << endl; 
    
while(cin >> ival) 
        ivec.push_back(ival);
    cin.clear(); //注意此处
    cin.ignore(std::numeric_limits
<streamsize>::max(),' ');//此处关键

    cout 
<< "the list's  numbers" << endl;
    
while(cin >> ival) 
        ilst.push_back(ival);
    vector 
<int> ::const_iterator vecit = ivec.begin(); 
    list  
<int> ::const_iterator lstit = ilst.begin(); 
    
if(ilst.size() != ivec.size()) 
    

         cout 
<< "they are not same" << endl;
         
return 0;    
     }
 
    
else 
    
{   
        
for( ; vecit != ivec.end(), lstit != ilst.end(); ++vecit, ++lstit) 
        

            
if(*vecit != *lstit) 
            
{  
                cout 
<< "they are not same" << endl; 
                
return 0
            }
 
         }
 
        cout 
<< "they are same!" << endl; 
    }
 
    
return 0
}
 
原创粉丝点击