1. ホーム
  2. c++

c++におけるcin.clear()とcin.sync()の使用法

2022-02-25 05:08:56
<パス

最近、あるプログラムを見ていたら、cinを使った後、cin.clear(); cin.sync()を使う必要がある場合があることがわかりました。
例えば、一連の整数を読み込んでvectorオブジェクトに入れ、count()で与えられた値の出現回数を数える場合、私が最初に作ったプログラムは次のようになります。

#include

#include"algorithm"
#include


using namespace std;

int main()
{
    int num;
    vector<int> ivec;
    while(cin>>num)
    {
        ivec.push_back(num);
    }
    cout<<"please input the number you want to count"<<<endl;
    cin>>num;
    cout<<num<<" appears "<<count(ivec.begin(), ivec.end(), num)<<" times"<<<endl;
    return 0;
}


2が発生する回数を数えるために、1 2 3 2の3つの数字を入力したところ、最終的に次のような出力が得られました。

1 2 3 2
^Z
please input the number you want to count
2 appears 2 times

	If you implement it yourself, you will find that when you finish entering 1 2 3 2 and press Ctrl+Z after please input the number you want to count will come out directly and then the number of times 2 appears, there is no chance to enter the number you want to count 2.
	This means that the input stream of the system as well as the data exists, the program will input the last data as the number you want to count. So we can clear the input stream, so we can enter cin.clear(); cin.sync(). cin.clear() is used to change the status identifier of cin, and cin.sync() is used to clear the data stream in the buffer. If the identifier is not changed, then even if the stream is cleared, it cannot be entered. So the two should be used together.
	The code after the change is as follows.

#include

#include"algorithm"
#include


using namespace std;

int main()
{
    int num;
    vector<int> ivec;
    while(cin>>num)
    {
        ivec.push_back(num);
    }
    cin.clear();
    cin.sync();
    cout<<"please input the number you want to count"<<<endl;
    cin>>num;
    cout<<num<<" appears "<<count(ivec.begin(), ivec.end(), num)<<" times"<<<endl;
    return 0;
}


	If you implement it yourself, you will find that when you finish entering 1 2 3 2 and press Ctrl+Z after please input the number you want to count will come out directly and then the number of times 2 appears, there is no chance to enter the number you want to count 2.
	This means that the input stream of the system as well as the data exists, the program will input the last data as the number you want to count. So we can clear the input stream, so we can enter cin.clear(); cin.sync(). cin.clear() is used to change the status identifier of cin, and cin.sync() is used to clear the data stream in the buffer. If the identifier is not changed, then even if the stream is cleared, it cannot be entered. So the two should be used together.
	The code after the change is as follows.


#include

#include"algorithm"
#include


using namespace std;

int main()
{
    int num;
    vector<int> ivec;
    while(cin>>num)
    {
        ivec.push_back(num);
    }
    cin.clear();
    cin.sync();
    cout<<"please input the number you want to count"<<<endl;
    cin>>num;
    cout<<num<<" appears "<<count(ivec.begin(), ivec.end(), num)<<" times"<<<endl;
    return 0;
}


もう一度実行すると、正しく、カウントしたい数字が入力されます。小さな問題に思えるかもしれませんが、注意しないとプログラムエラーの原因になります。