简体   繁体   中英

overloading >> operator for stack list class

LongInt i1; 

cin >> i1;

where LongInt is a class that contains a stack of integers. I want to store the input into the stack in the class and I assume that means I have to overload the >> operator in my class file. The problem is that I'm not sure how to push inputs like cin >> "111343241" into a stack digit by digit. How would I make this work?

To overload the operator itself, implementing the get loop, define a namespace-scope

std::istream& operator >>(std::istream& is, LongInt& li) {
    char c;
    while( is.get(c).good() ) {
        if( !std::isdigit(c) ) {
             is.unget();
             break;
        }

        ...push it to li
    }

    return is;
}

How you push single digits to the LongInt depends on your implementation of LongInt.

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