阅读https://www.boost.org/doc/libs/1_67_0/libs/tuple/doc/html/tuple_users_guide.html上的文章后 ,以下注释对我来说是个问题。请注意,使用 std::string 或 C 样式字符串元素提取元组通常不起作用,因为流式元组表示可能无法明确解析。我应该使用什么类型将字符串从流中明确解析为元组?
从元组中检索 std::string 时,该字符串由空格分隔。这是不希望的!设置分隔符,例如数字符号 (#) 没有帮助。
// typedef tuple
typedef std::string td_current_gmt, td_remote_endpoint,
td_request, td_response, td_elapsed_time;
typedef boost::tuples::tuple<td_current_gmt, td_remote_endpoint,
td_request, td_response, td_elapsed_time> tuple_logging;
// store in tuple
tuple_logging tl{ current_gmt, remote_endpoint,
request, response, elapsed_time };
// write tuple to file
tl = boost::tuples::make_tuple(current_gmt, remote_endpoint,
request, response, elapsed_time);
boost::filesystem::path p = { "logging" };
boost::filesystem::ofstream ofs{ p };
ofs << /*boost::tuples::set_delimiter('#') <<*/ tl;
ofs.close();
// read tuple from file
tuple_logging tlin{ current_gmt, remote_endpoint,
request, response, elapsed_time };
boost::filesystem::ifstream ifs{ p };
//ifs >> boost::tuples::set_delimiter('#');
ifs >> tlin;
输出是 (Fri, 16 Aug 2019 06:28:05) 但实际上它必须是 (Fri, 16 Aug 2019 06:28:05 GMT 192.168.178.14:52832 TRACE / HTTP/1.1 HTTP/1.1 200 OK 8.936800)