C++
输入/输出 | Input/output

std::ios_base::iword

科技促进发展:监督办[医]基::iword

long& iword( int index

首先,分配或调整私有存储%28动态数组的大小。long或另一个可索引的数据结构%29足以使index一个有效的索引,然后返回对long元素的私有存储的索引。index...

对此的任何操作都可能使引用无效。ios_base对象,包括对iword(),但是保留存储的值,以便从iword(index)使用相同的索引,以后将产生相同的值%28,直到下一次调用std::basic_ios::copyfmt()29%。该值可用于任何目的。元素的索引必须由xalloc()的其他用户之间的冲突。ios_base可能会发生。新元素初始化为​0​...

如果分配失败,调用std::basic_ios<>::setstate(badbit)可能会std::ios_base::failure...

注记

iword存储的典型用法是传递信息%28例如。自定义格式标志%29从用户定义的I/O操作器到用户定义的operator<<和operator>>或者是用户定义的格式方面,注入到标准流中。

参数

index-index value of the element

返回值

对元素的引用。

例外

可抛std::ios_base::failure当设置坏位时。

二次

#include <iostream> #include <string> struct Foo { static int foo_xalloc; std::string data; Foo(const std::string& s) : data(s) {} }; // allocates the iword storage for use with Foo objects int Foo::foo_xalloc = std::ios_base::xalloc( // This user-defined operator<< prints the string in reverse if the iword holds 1 std::ostream& operator<<(std::ostream& os, Foo& f) { if(os.iword(Foo::foo_xalloc) == 1) return os << std::string(f.data.rbegin(), f.data.rend() else return os << f.data; } // This I/O manipulator flips the number stored in iword between 0 and 1 std::ios_base& rev(std::ios_base& os) { os.iword(Foo::foo_xalloc) = !os.iword(Foo::foo_xalloc return os; } int main() { Foo f("example" std::cout << f << '\n' << rev << f << '\n' << rev << f << '\n'; }

二次

产出:

二次

example elpmaxe example

二次

另见

pwordresizes the private storage if necessary and access to the void* element at the given index (public member function)
xalloc staticreturns a program-wide unique integer that is safe to use as index to pword() and iword() (public static member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/IOS[医]基/字