mardi 24 décembre 2019

C++: Partial copy of a stringstream

How can I copy three characters beginning with the second one of str1 to str2? This code doesn't work:

#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>

int main()
{
  std::stringstream str1("0123456789");
  std::stringstream str2;

  str1.seekg(1, str1.beg);

  std::copy(std::istream_iterator<char>(str1), std::istream_iterator<char>(str1)+3, std::ostream_iterator<char>(str2));
  std::cout << str2.str() << '\n';
}

The expected output is:

123

This is a dummy test, but I'd like to work with larger stringstreams and efficient methods.

Aucun commentaire:

Enregistrer un commentaire