简体   繁体   中英

How to take space separated integers and chars as standard input in c++?

I need to take 1 2 + 3 4 - * as an input in c++, but couldn't figure out, and even the size is not given about how many chars.
Need to take it as input ans store it in a string without spaces.
Thanks in advance.

Use the getline function ( Source ) and the remove_if function ( Source ):

#include <iostream>
#include <algorithm>
#include <cctype>

int main(){
    std::string line;

    while (std::getline(std::cin, line)) {
        line.erase(std::remove_if(line.begin(), line.end(), ::isspace));
        //do whatever you want with `line`, it has no spaces anymore
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM