输入输出流的成员函数peekO:返回输入流中的下一个字符,但不将其从输入流中删除;形式 : cin.peekOgcounto:统计输入的字符个数;形式 : cin.gcountO10
输入输出流的成员函数 10 peek() :返回输入流中的下一个字符,但不 将其从输入流中删除; 形式:cin.peek(); gcount():统计输入的字符个数; 形式:cin.gcount();
例14-1比较cin和cin.get的输入int mainO const int SIZE = 80:char ch1,ch2,ch3,b1[SIZE],b2[SIZE]cout << "Enter a sentence:ln":cin >> bl;cout << "The string read with cin is:In" << bl:chl = cin.getOch2 = cin.getO:11
例14-1 比较cin和cin.get的输入 11 int main() { const int SIZE = 80; char ch1,ch2,ch3,b1[SIZE],b2[SIZE]; cout << "Enter a sentence:\n"; cin >> b1; cout << "The string read with cin is:\n" << b1; ch1 = cin.get(); ch2 = cin.get();
cout << "InThe result of cin.getO is:In" << chl <<ch2;cin.get(ch3);cout << "(nThe result of cin.get(ch3) is:In" << ch3;cin.get(b2,SIZE):cout << "\nThe string read with cin.get(b2,SIZEis:In" << b2 << endl:return O;12
12 cout << "\nThe result of cin.get() is:\n" << ch1 << ch2; cin.get(ch3); cout << "\nThe result of cin.get(ch3) is:\n" << ch3; cin.get(b2,SIZE); cout << "\nThe string read with cin.get(b2,SIZE) is:\n" << b2 << endl; return 0; }
1格式化I/014.4格式化数据的方式:使用操纵算子1、直接访问ios类的成员;2、一、流格式状态标志和格式化函数(1)流格式状态标志·ios:basefield(基数域)dec|oct|hexios::adjustfield(对齐域)left/rightinternalios::floatfield(浮点域)seientific|fixed13
14.4 格式化I/O 13 格式化数据的方式: 1、直接访问ios类的成员;2、使用操纵算子。 一、流格式状态标志和格式化函数 (1)流格式状态标志。 ios::basefield(基数域) dec|oct|hex ios::adjustfield(对齐域) left|right|internal ios::floatfield(浮点域) seientific|fixed
(2)设置标志的成员函数·setfO:设置流格式标志例 : cout.setf(ios::scientific);·unsetfO:清除流格式标志例 : cout.unsetf(ios::scientific);14
14 (2) 设置标志的成员函数 setf():设置流格式标志 例:cout.setf(ios::scientific); unsetf():清除流格式标志 例: cout.unsetf(ios::scientific);