C++/Console/cout manipulator
Содержание
- 1 bell manipulator (using escape sequence \a)
- 2 Define function to set cout
- 3 endLine manipulator (using escape sequence \n and the flush member function)
- 4 Predefine format for cout
- 5 Redirecting Standard Input And Output
- 6 ret manipulator (using escape sequence \r)
- 7 Set cout format: ios::showpoint | ios::uppercase | ios::scientific
- 8 Set cout format: ios::showpos
- 9 Show time and date.
- 10 Skip 10 characters.
- 11 tab manipulator (using escape sequence \t)
- 12 Turn on hex output with uppercase X.
- 13 Utility function for cin
bell manipulator (using escape sequence \a)
#include <iostream>
using std::ostream;
using std::cout;
using std::flush;
ostream& bell( ostream& output ) { return output << "\a"; }
int main()
{
cout << bell;
return 0;
}
Define function to set cout
#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;
}
endLine manipulator (using escape sequence \n and the flush member function)
#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;
}
Predefine format for cout
#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;
}
Redirecting Standard Input And Output
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string line;
int number = 0;
while( getline( cin, line))
{
cout << setw(5) << ++number << ": "
<< line << endl;
}
return 0;
}
ret manipulator (using escape sequence \r)
#include <iostream>
using std::ostream;
using std::cout;
using std::flush;
ostream& ret( ostream& output ) { return output << "\r"; }
int main()
{
cout << ret << "-----";
return 0;
}
Set cout format: ios::showpoint | ios::uppercase | ios::scientific
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpoint | ios::uppercase | ios::scientific);
cout << 100.0;
return 0;
}
Set cout format: ios::showpos
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos);
cout << -10 << " " << 10 << "\n";
return 0;
}
Show time and date.
#include <iostream>
#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;
}
Skip 10 characters.
#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;
}
tab manipulator (using escape sequence \t)
#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;
}
Turn on hex output with uppercase X.
#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;
}
Utility function for cin
#include <iostream>
#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;
}