C++の中でskipws、wsの両者はどんな違いがあります

890 ワード

C++の中でskipws、wsの両者はどんな違いがあります
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
 
int main(){
    istringstream istr(" 123 - 123");
    int a;
    istr >> noskipws >> a;
    if (!istr){
        cerr << "    
"; istr.clear(); } istr >> skipws >> a; cout << "a = " << a << "
"; char c; if (istr.get(c), c != '-'){ cerr << " -
"; istr.unget(); } if ((istr >> ws).get(c), c == '-'){ istr >> a; cout << "a = " << a << "
"; } return 0; }
skipwsは、>>でデータを読み込む前にスペースをスキップすることを意味します(この場合もスペースは存在します).wsは、後ろのすべてのスペースを読み取ることを意味します(スペースはスキップされます).次の例では、推測してみてください.