输入数据的方法

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    // 输入一串连续的数字并存放在vector数组中
    // input : 12345678
    // output: 1 2 3 4 5 6 7 8
    if (false)
    {
        string s;
        cin >> s;
        vector data;

        for (unsigned int i = 0; i :: iterator it = data.begin(); it != data.end(); it ++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }
    // 将一行带空格的数字存放在vector数组中  方法1
    // input : 12 34 56 78
    // output: 12 34 56 78
    if (false)
    {
        vector data;
        char ch;
        while ((ch = getchar()) != '\n')
        {
            if (ch >= '0' && ch <= '9')
            {
                int tmp = 0;
                ungetc(ch, stdin); // 通过getchar函数获得的字符送回缓冲区,再通过cin函数取出作为int型数组
                cin >> tmp;
                data.push_back(tmp);
            }
        }
        for (vector :: iterator it = data.begin(); it != data.end(); it ++)
        {
            cout << *it << " ";
        }
        //cout << endl;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             )
    }
    // 将一行带空格的数字存放在vector数组中  方法1
    // input : 12 34 56 78
    // output: 12 34 56 78
    if (true)
    {
        vector data;
        char ch;
        string str = "";

        while ((ch = getchar()) != '\n')
        {
            if (ch >= '0' && ch <= '9')
            {
                str += ch;
            }
            else if (ch == ' ')
            {
                int tmp = atoi(str.c_str());
                data.push_back(tmp);
                str = "";
            }
        }

        for (vector :: iterator it = data.begin(); it != data.end(); it ++)
        {
            cout << *it << " ";
        }
        //cout << endl;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             )
    }

    return 0;
}

 

你可能感兴趣的:(剑指offer)