ostream之cout

xiaoxiao2021-02-27  185

#include<iostream> #include<cstring> #include<fstream>//文件流 #include<sstream>//字符串流 using namespace std; class Time { public: Time(int year, int mon, int day, int hour, int min, int sec) :_year(year), _mon(mon), _day(day), _hour(hour), _min(min), _sec(sec) { } friend void outTime(ostream &, const Time &); private: int _year; int _mon; int _day; int _hour; int _min; int _sec; }; void outTime(ostream &os, const Time &tm) { os << dec << right; os << tm._year << '.'; os.width(2); os.fill('0'); os << tm._mon << '.'; os.width(2); os.fill('0'); os << tm._day << '\t'; os.width(2); os.fill('0'); os << tm._hour << ':'; os.width(2); os.fill('0'); os << tm._min << ':'; os.width(2); os.fill('0'); os << tm._sec << endl; } void main() { cout.put('a'); char buf[] = "blank ! \n"; cout.write(buf, strlen(buf)); cout.width(10); cout.fill('$'); cout << 222 << endl; cout << "b"; cout.setf(ios::hex, ios::basefield); cout << hex << left << 66 << endl; cout << boolalpha << true << endl; outTime(cout, Time(2013, 10, 5, 6, 2, 10)); fstream fs; fs.open("time.txt", ios::out); outTime(fs, Time(2013, 4, 5, 6, 7, 8)); fs.close(); stringstream strs; //会以空白字符作为截断 outTime(strs, Time(2013, 4, 5, 6, 7, 8)); string str; strs >> str; cout << str;//输出第一部分2013:04:05 cout << "------------"; strs >> str; cout << str;//输出第二部分06:07:08 cout << endl; }
转载请注明原文地址: https://www.6miu.com/read-13346.html

最新回复(0)