function_output_iterator_eg.rst 751 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. .. Copyright David Abrahams 2006. Distributed under the Boost
  2. .. Software License, Version 1.0. (See accompanying
  3. .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. Example
  5. .......
  6. ::
  7. struct string_appender
  8. {
  9. string_appender(std::string& s)
  10. : m_str(&s)
  11. {}
  12. void operator()(const std::string& x) const
  13. {
  14. *m_str += x;
  15. }
  16. std::string* m_str;
  17. };
  18. int main(int, char*[])
  19. {
  20. std::vector<std::string> x;
  21. x.push_back("hello");
  22. x.push_back(" ");
  23. x.push_back("world");
  24. x.push_back("!");
  25. std::string s = "";
  26. std::copy(x.begin(), x.end(),
  27. boost::make_function_output_iterator(string_appender(s)));
  28. std::cout << s << std::endl;
  29. return 0;
  30. }