有一个参数的输出流操控器(int或更长)

iostream 类库提供用于创建参数化的操控器提供了一组宏。具有唯一 int 或 long 参数的操控器是一个特例。若要创建接受单个 int 或 long 参数的输出流操控器 (如 setw),必须使用 _Smanip 宏,定义 iomanip。此示例定义一个 fillblank 的操控器插入空白指定数目的到流中:

示例

// output_stream_manip.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <iomanip>
using namespace std;

void fb( ios_base& os, int l )
{
   ostream *pos = dynamic_cast<ostream*>(&os);
   if (pos)
   {
      for( int i=0; i < l; i++ )
      (*pos) << ' ';
   };
}

_Smanip<int>
   __cdecl fillblank(int no)
   {   
   return (_Smanip<int>(&fb, no));
   }

int main( )
{
   cout << "10 blanks follow" << fillblank( 10 ) << ".\n";
}

请参见

参考

具有参数的自定义操控器