insertsvar.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // inserts.cpp -- copy() and insert iterators
  2. #include <iostream>
  3. #include <string>
  4. #include <iterator>
  5. #include <vector>
  6. #include <algorithm>
  7. void output(const std::string & s) {std::cout << s << " ";};
  8. void outc(char ch) {std::cout << ch; }
  9. void routput(const std::string & s) {std::for_each(s.rbegin(),s.rend(),outc); std::cout << " ";}
  10. int main()
  11. {
  12. using namespace std;
  13. string s1[4] = {"fine", "fish", "fashion", "fate"};
  14. string s2[2] = {"busy", "bats"};
  15. string s3[2] = {"silly", "singers"};
  16. vector<string> words(4);
  17. copy(s1, s1 + 4, words.begin());
  18. for_each(words.begin(), words.end(), output);
  19. cout << endl;
  20. // construct anonymous back_insert_iterator object
  21. copy(s2, s2 + 2, back_insert_iterator<vector<string> >(words));
  22. for_each(words.begin(), words.end(), output);
  23. cout << endl;
  24. // construct anonymous insert_iterator object
  25. copy(s3, s3 + 2, insert_iterator<vector<string> >(words, words.begin()));
  26. for_each(words.begin(), words.end(), output);
  27. cout << endl;
  28. for_each(words.begin(), words.end(), routput);
  29. cout << endl;
  30. // cin.get();
  31. return 0;
  32. }