fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. int main() {
  7. const std::string str { "123456789" };
  8. std::vector<char> sub;
  9. auto iter = std::begin(str);
  10. // use advance to make this clear when you read.
  11. std::advance(iter, 3);
  12. // from where the advance left the iterator, and go fw 4 positions.
  13. std::copy_n(iter, 4, std::back_inserter(sub));
  14. for (auto c: sub) {
  15. std::cout << c << "\n";
  16. }
  17. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
4
5
6
7