C ++自定义流操纵器,改变下一个项目

问题描述:

在C ++中,要以十六进制打印一个数字,请执行以下操作:

In C++, to print a number in hexadecimal you do this:

int num = 10;
std::cout << std::hex << num; // => 'a'

我知道我可以创建一个操纵器, / p>

I know I can create a manipulator that just adds stuff to the stream like so:

std::ostream& windows_feed(std::ostream& out)
{
    out << "\r\n";
    return out;
}

std::cout << "Hello" << windows_feed; // => "Hello\r\n"

但是,如何创建一个操纵器,十六进制,修改项目来流?作为一个简单的例子,我如何在这里创建plusone操纵器:

However, how can I create a manipulator that, like 'hex', modifies items to come on the stream? As a simple example, how would I create the plusone manipulator here?:

int num2 = 1;
std::cout << "1 + 1 = " << plusone << num2; // => "1 + 1 = 2"

// note that the value stored in num2 does not change, just its display above.
std::cout << num2; // => "1"


首先,到每个流。您可以使用函数 iword 以及您传递给它的索引,由 xalloc

First, you have to store some state into each stream. You can do that with the function iword and an index you pass to it, given by xalloc:

inline int geti() { 
    static int i = ios_base::xalloc();
    return i;
}

ostream& add_one(ostream& os) { os.iword(geti()) = 1; return os; } 
ostream& add_none(ostream& os) { os.iword(geti()) = 0; return os; }

有了这些,你可以在所有流中检索一些状态。现在,你只需要钩住相应的输出操作。数字输出由构面完成,因为它可能与语言环境相关。所以你可以做

Having that in place, you can already retrieve some state in all streams. Now, you just have to hook into the respective output operation. Numeric output is done by a facet, because it potentially is locale dependent. So you can do

struct my_num_put : num_put<char> {
    iter_type 
    do_put(iter_type s, ios_base& f, char_type fill, long v) const { 
        return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); 
    } 

    iter_type 
    do_put(iter_type s, ios_base& f, char_type fill, unsigned long v) const { 
        return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); 
    } 
}; 

现在,您可以测试这些东西了。

Now, you can test the stuff.

int main() {
    // outputs: 11121011
    cout.imbue(locale(locale(),new my_num_put));
    cout << add_one << 10 << 11 
         << add_none << 10 << 11;
}

如果希望只有下一个数字递增, 0 每次调用 do_put 后再次

If you want that only the next number is incremented, just set the word to 0 again after each call to do_put.