C++ Tutorial/Development/output manipulator

Материал из C\C++ эксперт
Версия от 10:28, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A simple output manipulator

#include <iostream>
#include <iomanip>
using namespace std;
ostream &sethex(ostream &stream)
{
  stream.setf(ios::showbase);
  stream.setf(ios::hex, ios::basefield);
  return stream;
}
int main()
{
  cout << 256 << " " << sethex << 256;
  return 0;
}
256 0x100"

Create an output manipulator

#include <iostream> 
#include <iomanip> 
using namespace std; 
 
ostream &mySetting(ostream &stream) 
{ 
  stream.setf(ios::left); 
  stream << setw(10) << setfill("$"); 
  return stream; 
} 
 
int main() 
{ 
  cout << 10 << " " << mySetting << 10; 
 
  return 0; 
}
10 10$$$$$$$$

Create custom ostream operator

#include <iostream>
#include <iomanip>
using namespace std; 
ostream &sethex(ostream &stream){
   stream.setf(ios::showbase);
   stream.setf(ios::hex);
   return stream;
}
int main(void){
   cout << 256 << "   " << sethex << 256;
}

Create user-defined, nonparameterized stream manipulators: bell manipulator (using escape sequence \a)

#include <iostream>
using std::ostream;
using std::cout;
using std::flush;
ostream& bell( ostream& output )
{
   return output << "\a";  // issue system beep
} 
int main()
{
   cout << bell; // use bell manipulator
   return 0;
}

Create user-defined, nonparameterized stream manipulators: carriageReturn manipulator (using escape sequence \r)

#include <iostream>
using std::ostream;
using std::cout;
using std::flush;

ostream& carriageReturn( ostream& output )
{
   return output << "\r";  // issue carriage return
}
int main()
{
   cout << carriageReturn << "-----" ;
   return 0;
}

EndLine manipulator (using escape sequence \n and member function flush)

#include <iostream>
using std::ostream;
using std::cout;
using std::flush;
ostream& endLine( ostream& output )
{
   return output << "\n" << flush; // issue endl-like end of line
}
int main()
{
   cout << "-----" << endLine;
   return 0;
}
-----

ostream flag

#include <iostream>
#include <iomanip>
using namespace std;
ostream &ra(ostream &stream)
{
  stream << "-------------- ";
  return stream;
}
// Left Arrow
ostream &la(ostream &stream)
{
  stream << "*************";
  return stream;
}
int main()
{
  cout << ra << 1233.23 << "\n";
  cout << ra << 567.66 << la;
  return 0;
}
-------------- 1233.23
-------------- 567.66*************"

tab manipulator (using escape sequence \t)

#include <iostream>
using std::ostream;
using std::cout;
using std::flush;
ostream& tab( ostream& output )
{
   return output << "\t";  // issue tab
}
int main()
{
   cout << tab << "b" << tab << "c" ;
   return 0;
}
b       c

Using the endl stream manipulator.

#include <iostream>
using namespace std;
int main()
{
   cout << "Welcome to ";
   cout << "C++!";
   cout << endl;   // end line stream manipulator
   return 0;
}