操纵符是令代码能以 operator<< 或 operator>> 控制输入/输出流的帮助函数。
不以参数调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex; )实现为接受到流的引用为其唯一参数的函数。 basic_ostream::operator<< 和 basic_istream::operator>> 的特别重载版本接受指向这些函数的指针。这些函数(或函数模板的实例化)是标准库中仅有的可取址函数。 (C++20 起)
以参数调用的操纵符(例如 std::cout << std::setw(10); )实现为返回未指定类型对象的函数。这些操纵符定义其自身的进行请求操作的 operator<< 或 operator>> 。
定义于头文件
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 注意:不写入 |
std::get_time
| template< class CharT > | (C++11 起) |
用于表达式 in >> get_time(tmb, fmt) 时,按照格式字符串 fmt 及输入流 in 中当前感染的 locale 的 std::time_get 平面,分析字符输入为日期/时间值。存储结果值于 tmb 所指向的 std::tm 对象中。
| tmb | - | 指向 std::tm 对象的合法指针,结果将存储于其处 |
| fmt | - | 指向指定转换格式的空终止 CharT 字符串的指针 格式字符串由零或更多转换指定符、空白符和通常字符(除了 |
返回未指定类型的对象,使得若 in 为 std::basic_istream
- typedef std::istreambuf_iterator<CharT, Traits> Iter;
- typedef std::time_get<CharT, Iter> TimeGet;
- std::ios_base::iostate err = std::ios_base::goodbit;
- const TimeGet& tg = std::use_facet<TimeGet>(in.getloc());
- tg.get(Iter(in.rdbuf()), Iter(), in, err, tmb, fmt, fmt + traits::length(fmt));
- if (err != std::ios_base::goodbit)
- in.setstate(err);
- #include <iostream>
- #include <sstream>
- #include <locale>
- #include <iomanip>
-
- int main()
- {
- std::tm t = {};
- std::istringstream ss("2011-Februar-18 23:12:34");
- ss.imbue(std::locale("de_DE.utf-8"));
- ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
- if (ss.fail())
- {
- std::cout << "Parse failed\n";
- }
- else
- {
- std::cout << std::put_time(&t, "%c") << '\n';
- }
- return 0;
- }
Sun Feb 18 23:12:34 2011
std::put_time
| template< class CharT > | (C++11 起) |
用于表达式 out << put_time(tmb, fmt) 时,按照格式字符串 fmt ,按照输出流 out 中当前感染的 locale 的 std::time_put 平面,转换来自给定的日历时间 tmb 的日期和时间信息为字符串,如同通过调用 std::strftime 、 std::wcsftime 或模拟(取决于 CharT )。
| tmb | - | 指向从 localtime() 或 gmtime() 获得的日历时间结构体的指针 |
| fmt | - | 指向指定转换格式的空终止 CharT 串的指针。 格式字符串由零或更多个限定符和通常字符(除 |
返回为指定类型的对象,使得若 out 为 std::basic_ostream
- typedef std::ostreambuf_iterator<CharT, Traits> Iter;
- typedef std::time_put<CharT, Iter> TimePut;
- const TimePut& tp = std::use_facet<TimePut>(out.getloc());
- const Iter end = tp.put(Iter(out.rdbuf()), out, out.fill(), tmb, fmt, fmt + Traits::length(fmt));
- if (end.failed())
- out.setstate(std::ios_base::badbit);
- #include <iostream>
- #include <iomanip>
- #include <ctime>
-
- int main()
- {
- std::time_t t = std::time(nullptr);
- std::tm tm = *std::localtime(&t);
- // std::cout.imbue(std::locale("ru_RU.utf8"));
- std::cout << "ru_RU: " << std::put_time(&tm, "%c %Z") << std::endl;
- // std::cout.imbue(std::locale("ja_JP.utf8"));
- std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << std::endl;
- return 0;
- }
- ru_RU: Ср. 28 дек. 2011 10:21:16 EST
- ja_JP: 2011年12月28日 10時21分16秒 EST