func_output_iter_example.cpp 900 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // (C) Copyright Jeremy Siek 2001-2004.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Revision History:
  6. // 27 Feb 2001 Jeremy Siek
  7. // Initial checkin.
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. #include <boost/function_output_iterator.hpp>
  12. struct string_appender
  13. {
  14. string_appender(std::string& s)
  15. : m_str(&s)
  16. {}
  17. void operator()(const std::string& x) const
  18. {
  19. *m_str += x;
  20. }
  21. std::string* m_str;
  22. };
  23. int main(int, char*[])
  24. {
  25. std::vector<std::string> x;
  26. x.push_back("hello");
  27. x.push_back(" ");
  28. x.push_back("world");
  29. x.push_back("!");
  30. std::string s = "";
  31. std::copy(x.begin(), x.end(),
  32. boost::make_function_output_iterator(string_appender(s)));
  33. std::cout << s << std::endl;
  34. return 0;
  35. }