以std::copy为例,下面的代码将容器(list)中的字符串按行输出到指定的文件,只要2行代码:
#include
#include
/* 迭代器指定的字符串写入指定的文件...begin 起始迭代器
* end 结束迭代器
*/
template
inline bool save_container_to_text(const std...::string&filename, inIter begin, inIter end) {
std::ofstream fout(filename, std::ofstream::binary);...std::copy(begin, end, std::ostream_iteratorstd::string>(fout, "\n"));
// 不需要显式调用open(),close(),fout...open,fout对象析构时会自动执行close()函数
return true;
}
调用示例:
list container;
// container 可以为list,map,vector