更改流上的下一项的 C++ 自定义流操纵器 [英] C++ custom stream manipulator that changes next item on stream

查看:24
本文介绍了更改流上的下一项的 C++ 自定义流操纵器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,要以十六进制打印数字,您可以这样做:

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

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

我知道我可以创建一个操纵器,它只是向流中添加内容,如下所示:

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

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

std::cout << "Hello" << windows_feed; // => "Hello
"

但是,我如何创建一个操纵器,像十六进制"那样修改流中的项目?作为一个简单的例子,我将如何在这里创建 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<给出/a>:

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; }

有了它,您就可以在所有流中检索某些状态.现在,您只需挂钩相应的输出操作.数字输出由 facet 完成,因为它可能与区域设置相关.所以你可以这样做

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;
}

如果您只想增加下一个数字,只需在每次调用 do_put 后再次将单词设置为 0.

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

这篇关于更改流上的下一项的 C++ 自定义流操纵器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆