The manipulators which have an effect when used on standard istream objects are:
// 以数字形式或true false形式输出bool值
//example manipulator1.cpp
boolalpha |
noboolalpha |
skipws |
noskipws | Do not skip whitespaces (manipulator function) |
dec | Use decimal base (manipulator function) |
hex | Use hexadecimal base (manipulator function) |
oct | Use octal base (manipulator function) |
ws | Extract whitespaces (manipulator function) |
//example manipulator1.cpp
#include <iostream>
using namespace std;
int main ()
{
bool b;
b = true;
bool b2;
b2 = false;
cout << boolalpha << b << endl;
cout << b2 << endl; // boolalpha 操作符一直影响cout
cout << noboolalpha << b << endl;
return 0;
}
////// result
123
1
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// result
true
false
1
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// example manipulator2.cpp
#include <iostream>
#include <sstream>
//#include <string>
using namespace std;
int main () {
char a, b, c;
// 忽略空白,从字串流中一个一个读取字母
istringstream iss (" 1 2 3");
// string iss (" 123"); // error
iss >> skipws >> a >> b >> c;
cout << a << b << c << endl;
iss.seekg(0);
iss >> noskipws >> a >> b >> c;
cout << a << b << c << endl;
return 0;
}
//////////////////////////////////////////////////////
setiosflags
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
cout << hex << setiosflags (ios_base::showbase | ios_base::uppercase);
cout << 100 << endl;
return 0;
}
/////////////////////////////////////////////////////////////////
// setbase
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
cout << setbase (16);
cout << 200 << endl;
return 0;
}
// c8
/////////////////////////////////////////////////////////////////
// limit width
#include <iostream>
using namespace std;
int main ()
{
char str[10];
cout << "Enter a word: ";
cin.width (10); // limit width
cin >> str;
cout << "The first 9 chars of your word are: " << str << endl;
return 0;
}