C++/Console/cout manipulator

Материал из C\C++ эксперт
Перейти к: навигация, поиск

bell manipulator (using escape sequence \a)

<source lang="cpp">

  1. include <iostream>

using std::ostream; using std::cout; using std::flush;

ostream& bell( ostream& output ) { return output << "\a"; } int main() {

  cout << bell;
  return 0;

}


 </source>


Define function to set cout

<source lang="cpp">

  1. include <iostream>

using namespace std; ostream &setsci(ostream &stream) {

 stream.setf(ios::scientific | ios::uppercase);
 
 return stream;

} int main() {

 double f = 123.23;
 cout << setsci << f;
 cout << "\n";
 return 0;

}


 </source>


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

<source lang="cpp">

  1. include <iostream>

using std::ostream; using std::cout; using std::flush; ostream& endLine( ostream& output ) {

  return output << "\n" << flush;

} // end function endLine int main() {

  cout << "T" << endLine
       << endLine
       << endLine << "..........";
  return 0;

}


 </source>


Predefine format for cout

<source lang="cpp">

  1. include <iostream>

using namespace std; ostream &tabs(ostream &stream) {

 stream << "\t" << "\t" << "\t" ;
 stream.width(20);
 return stream;

} int main() {

 cout << tabs << "Testing\n";
 return 0;

}


 </source>


Redirecting Standard Input And Output

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>
  3. include <string>

using namespace std; int main() {

  string line;
  int number = 0;
  while( getline( cin, line))
  {                             
    cout << setw(5) << ++number << ": "
         << line << endl;
  }
  return 0;

}


 </source>


ret manipulator (using escape sequence \r)

<source lang="cpp">

  1. include <iostream>

using std::ostream; using std::cout; using std::flush; ostream& ret( ostream& output ) { return output << "\r"; } int main() {

  cout << ret << "-----";
  return 0;

}


 </source>


Set cout format: ios::showpoint | ios::uppercase | ios::scientific

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout.setf(ios::showpoint | ios::uppercase | ios::scientific);
 cout << 100.0;
 return 0;

}


 </source>


Set cout format: ios::showpos

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout.setf(ios::showpos);
 cout << -10 << " " << 10 << "\n";
 
 return 0;

}


 </source>


Show time and date.

<source lang="cpp">

  1. include <iostream>
  2. include <ctime>

using namespace std; // A time and date output manipulator. ostream &td(ostream &stream) {

 struct tm *localt;
 time_t t;
 
 t = time(NULL);
 localt = localtime(&t);
 stream << asctime(localt) << endl;
 return stream;

} int main() {

 cout << td << "\n";
 return 0;

}


 </source>


Skip 10 characters.

<source lang="cpp">

  1. include <iostream>

using namespace std;

istream &skipchar(istream &stream) {

 int i;
 char c;
 for(i = 0; i <10; i++) stream >> c;
 return stream;

} int main() {

 char str[80];
 cout << "Enter some characters: ";
 cin >> skipchar >> str;
 cout << str << "\n";
 return 0;

}


 </source>


tab manipulator (using escape sequence \t)

<source lang="cpp">

  1. include <iostream>

using std::ostream; using std::cout; using std::flush; ostream& tab( ostream& output ) { return output << "\t"; } int main() {

  cout << "a" << tab << "b" << tab << "c" ;
  return 0;

}


 </source>


Turn on hex output with uppercase X.

<source lang="cpp">

  1. include <iostream>

using namespace std;

ostream &sethex(ostream &stream) {

 stream.unsetf(ios::dec | ios::oct);
 stream.setf(ios::hex | ios::uppercase | ios::showbase);
 return stream;

} // Reset flags. ostream &reset(ostream &stream) {

 stream.unsetf(ios::hex | ios::uppercase | ios::showbase);
 stream.setf(ios::dec);
 return stream;

} int main() {

 cout << sethex << 100 << "\n";
 cout << reset << 100 << "\n";
 return 0;

}


 </source>


Utility function for cin

<source lang="cpp">

  1. include <iostream>
  2. include <cctype>

using namespace std; istream &findalpha(istream &stream) {

 char ch;
 do {
   stream.get(ch);
 } while(!isalpha(ch));
 return stream;

} int main() {

 char str[80];
 cin >> findalpha >> str;
 cout << str << "\n";
 return 0;

}


 </source>