C + + Input CTRL + Z no more problems with CIN

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.