带有一个自变量(int 或 long)的输出流操控器

iostream 类库提供用于创建参数化操控器的一组宏。 具有单个 intlong 参数的操控器是一种特殊情况。 若要创建接受单个 intlong 参数(如 setw)的输出流操控器,必须使用在 <iomanip> 中定义的 _Smanip 宏。 此示例定义了向流中插入指定数量的空格的 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";
}

另请参阅

带自变量的自定义操控器