C ++: string :: cin Summary


1. Background

  Learning C ++, cin is inevitable, now for some cin summary.

2. Summary

  1. It is an istream cin, for reading data from the standard input. Specific process: - Save> to buffer data - the keyboard input data> CIN buffer is read.
  2. cin.sync () for emptying the buffer.
  3. cin.clear () cin for modifying the status identifiers.

3. Example

3.1 once cin

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

int main()
{
    string s1, s2;
    cin >> s1;
    cout << s1 << endl;
    return 0;
}

operation result

Description: When the input "1234" for row, beginning with CIN ignore white space (spaces, line breaks, tabs, etc.), read from the valid data, i.e., from "1", it encounters the next blank at the reading will end, that is read only to "2." cin is not normal end, because the buffer also retains data: "34" and line breaks.

3.2 cin twice

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

int main()
{
    string s1, s2;
    cin >> s1;
    cout << s1 << endl;

    cout << "----------------------------" << endl;
    cin >> s2;
    cout << s2 << endl;

    return 0;
}

operation result

Note:
1. When the input "1234" for row, the program directly outputs "12" and "34", people will not continue to input s2. The reason is that the input s1, cin has "12" to read, but still left in the buffer "34" and line breaks. When the next input, cin has default read "34", the end of the encounter line breaks.
2. Enter the correct approach should be to enter "12" line feed, and then enter "34" wraps. Cin can wrap the end of the normal read data.

Enter the correct way of operating results

3. Insert cin.sync () clear the buffer data, it is possible to achieve the correct input twice. Since the "12" on a read cin, the buffer is emptied. Cin second read data from the buffer, data is empty, the need to re-enter.

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

int main()
{
    string s1, s2;
    cin >> s1;
    cout << s1 << endl;

    cin.sync(); //清空缓冲区数据
    cout << "----------------------------" << endl;

    cin >> s2;
    cout << s2 << endl;
    return 0;
}

operation result

3.3 After the end of file (Ctrl + Z) cin

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

int main()
{
    string s1, s2, s3;
    while (cin >> s1)//对输入的字符串进行连接
    {
        s2 += s1;
    }
    cout << s2 << endl;

    cout << "------------------------" << endl;

    cin >> s3;
    cout << s3 << endl;

    return 0;
}

operation result

Description: cin enter circulation, and then the loop is exited with Ctrl + Z, cin later found ineffective. The reason for this is Ctrl + Z has made cin failure. Available cin.clear () cin modified state identifier, so cin available again.

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

int main()
{
    string s1, s2, s3;
    while (cin >> s1)//对输入的字符串进行连接
    {
        s2 += s1;
    }
    cout << s2 << endl;

    cin.clear(); //修改cin状态标识符

    cout << "------------------------" << endl;
    getline(cin, s1);
    for (unsigned int i = 0; i < s1.size(); i++)
    {
        if (s1[i] != ' ')
        {
            s3 += s1[i];
        }
    }
    cout << s3 << endl;

    return 0;
}

operation result

Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103927910