c++11:std::copy例

3442 ワード

c++11のライブラリは、多くの基礎的に有用なテンプレート関数を提供します.std::copyを例にとると、次のコードは、コンテナ(list)の文字列を2行のコードで指定されたファイルに行で出力します.
#include 
#include 
/*                 ,    
* filename * begin * end */
template< typename inIter > 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_iterator<std::string>(fout, "
"
)); // open(),close(),fout open,fout close() return true; }

呼び出し例:
list<string> container;
// container    list,map,vector     
save_container_to_text("output.txt", container.begin(), container.end());