简体   繁体   English

重载 >> 堆栈列表的运算符 class

[英]overloading >> operator for stack list class

LongInt i1; 

cin >> i1;

where LongInt is a class that contains a stack of integers.其中 LongInt 是一个 class,它包含一个整数栈。 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.我想将输入存储到 class 中的堆栈中,我认为这意味着我必须在我的 class 文件中重载 >> 运算符。 The problem is that I'm not sure how to push inputs like cin >> "111343241" into a stack digit by digit.问题是我不确定如何将像 cin >> "111343241" 这样的输入逐位推入堆栈。 How would I make this work?我将如何进行这项工作?

To overload the operator itself, implementing the get loop, define a namespace-scope要重载运算符本身,实现 get 循环,定义一个命名空间范围

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.如何将单个数字推送到 LongInt 取决于您对 LongInt 的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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