Problem Description: The procedure is to start writing data to the container, CTRL + Z to terminate the input stream, and then need to enter a data to determine whether there is this data in the container.
The source code is as follows:
1#include <iostream>2#include <map>3#include <string>4#include <vector>5 using namespacestd;6 intMain ()7 {8map<string,vector<int> >My_map;9 stringWord;Ten stringWord_to_find; Onevector<int>Ivec; A while(Cin >>word) -My_map[word].push_back (0); - thecout <<"input A string want to find:"<<Endl; - -CIN >>Word_to_find; -map<string,vector<int> >::iterator it =My_map.find (word_to_find); + if(It! =my_map.end ()) - { +vector<int>::iterator It_vec = (it->second). Begin (); Acout << It->first <<Endl; at for(It_vec;it_vec! = (It->second). end (); it_vec++) -cout << *it_vec <<" "; -cout <<Endl; - } -}
When you enter CTRL + Z to terminate the input stream on line 12th, the CIN statement on line 17th is invalidated, and the default program of the Z-CTRL (file terminator) is no longer required, so the following CIN statement is invalid.
I started the idea is whether CTRL + Z or stay in the buffer, need to refresh the buffer (clear data flow) to re-enter, so in the position of 14 lines to join cout << Endl; found that this is not useful, after searching online, found that the following solutions are Need to add cin.clear () and Cin.sync ().
1#include <iostream>2#include <map>3#include <string>4#include <vector>5 using namespacestd;6 intMain ()7 {8map<string,vector<int> >My_map;9 stringWord;Ten stringWord_to_find; Onevector<int>Ivec; A while(Cin >>word) -My_map[word].push_back (0); - theCin.clear ();//Change the status identifier for CIN -Cin.sync ();//clearing the data stream of a buffer -cout <<"input A string want to find:"<<Endl; - +CIN >>Word_to_find; -map<string,vector<int> >::iterator it =My_map.find (word_to_find); + if(It! =my_map.end ()) A { atvector<int>::iterator It_vec = (it->second). Begin (); -cout << It->first <<Endl; - for(It_vec;it_vec! = (It->second). end (); it_vec++) -cout << *it_vec <<" "; -cout <<Endl; - } in}
C + + Input CTRL + Z no more problems with CIN