read input separated by whitespace(s) or newline…?

后端 未结 6 971
日久生厌
日久生厌 2020-12-13 00:50

I\'m grabbing input from a standard input stream. Such as,

1 2 3 4 5

or

1
2
3
4
5

I\'m using:

<         


        
相关标签:
6条回答
  • 2020-12-13 01:20
    int main()
    {
        int m;
        while(cin>>m)
        {
        }
    }
    

    This would read from standard input if it space separated or line separated .

    0 讨论(0)
  • 2020-12-13 01:40

    Just use:

    your_type x;
    while (std::cin >> x)
    {
        // use x
    }
    

    operator>> will skip whitespace by default. You can chain things to read several variables at once:

    if (std::cin >> my_string >> my_number)
        // use them both
    

    getline() reads everything on a single line, returning that whether it's empty or contains dozens of space-separated elements. If you provide the optional alternative delimiter ala getline(std::cin, my_string, ' ') it still won't do what you seem to want, e.g. tabs will be read into my_string.

    Probably not needed for this, but a fairly common requirement that you may be interested in sometime soon is to read a single newline-delimited line, then split it into components...

    std::string line;
    while (std::getline(std::cin, line))
    {
        std::istringstream iss(line);
        first_type first_on_line;
        second_type second_on_line;
        third_type third_on_line;
        if (iss >> first_on_line >> second_on_line >> third_on_line)
            ...
    }
    
    0 讨论(0)
  • 2020-12-13 01:43

    the user pressing enter or spaces is the same.

    int count = 5;
    int list[count]; // array of known length
    cout << "enter the sequence of " << count << " numbers space separated: ";
    // user inputs values space separated in one line.  Inputs more than the count are discarded.
    for (int i=0; i<count; i++) {
        cin >> list[i];
    }
    
    0 讨论(0)
  • 2020-12-13 01:45

    Use 'q' as the the optional argument to getline.

    #include <iostream>
    #include <sstream>
    
    int main() {
        std::string numbers_str;
        getline( std::cin, numbers_str, 'q' );
    
        int number;
        for ( std::istringstream numbers_iss( numbers_str );
              numbers_iss >> number; ) {
            std::cout << number << ' ';
        }
    }
    

    http://ideone.com/I2vWl

    0 讨论(0)
  • 2020-12-13 01:45

    std::getline( stream, where to?, delimiter ie

    std::string in;
    std::getline(std::cin, in, ' '); //will split on space
    

    or you can read in a line, then tokenize it based on whichever delimiter you wish.

    0 讨论(0)
  • 2020-12-13 01:46
    #include <iostream>
    
    using namespace std;
    
    string getWord(istream& in) 
    {
        int c;
    
        string word;
    
        // TODO: remove whitespace from begining of stream ?
    
        while( !in.eof() ) 
        {
    
            c = in.get();
    
            if( c == ' ' || c == '\t' || c == '\n' ) break;
    
            word += c;
        }
    
        return word;
    }
    
    int main()
    {
        string word;
    
        do {
    
            word = getWord(cin);
    
            cout << "[" << word << "]";
    
        } while( word != "#");
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题