C++ custom stream manipulator that changes next item on stream(更改流上的下一项的 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++ 自定义流操纵器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改流上的下一项的 C++ 自定义流操纵器
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
