简体   繁体   中英

How can I convert the output from a ostream_iterator to a string?

I am implementing a toString method in C++ primarily using the ostream_iterator.

std::ostream_iterator<int> output(std::cout, " ");

After printing to the console, however, I wish to return the output of the ostream_iterator as a string. Is this direct conversion possible?

Is this direct conversion possible?

I think, no, because ostream_iterator is used to send data to stream one way, so if the destination is std::cout you cannot use ostream_iterator to get data back.

But you can use some other destination, eg ostringstream object, and then use it for both output through cout and use again as you like.

Consider simple example:

    std::stringstream buff;
    std::ostream_iterator<int> output(buff, " ");
    std::istringstream str("10 3 ABC 7 1");
    // just copy only first integer values from str (stop on the first non-integer)
    std::copy(std::istream_iterator<int>(str), std::istream_iterator<int>(), output);
    // output as a string to std output
    std::cout << buff.str() << std::endl;
    // do something else
    buff.seekp(0, std::ios::end);
    std::cout << "String size is " << buff.tellp() << std::endl;

Yes, just replace cout with stringstream and you can create string from it.

std::stringstream ss; // #include <sstream>
std::ostream_iterator<int> output(ss, " ");
std::string s(ss.str());

If you want a string-like object that uses the std::ostream interface rather than the STL container interface, that's std::stringstream , or the deprecated std::strstream The closest thing to a generic wrapper that turns anything that can be serialized with cout << into a std::string would be to write to a std::stringstream and then read the data back out. This will end up making at least one expensive copy, however. Here's an implementation:

#include <cstdlib>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using isit = std::istream_iterator<char>;

template <typename T> std::string to_string(const T& x)
// Must be able to print a T with std::ostream::operator<<.
{
  std::stringstream buffer;
  buffer << x;

  const isit begin(buffer);     // The start of the stream object. 
  static const isit end;        // Default object is the end-of-stream iterator.

  return std::string( begin, end );
}

int main(void)
{
  const std::string output_string = to_string("Testing:") + " " +
    to_string(1) + ", " + to_string(2ULL) + ", " + to_string(3.0) +
    to_string('.');

  cout << output_string << endl;
  return EXIT_SUCCESS;
}

You can use a std::ostream_iterator<char> to write to a std::stringstream , rather than << , and then a std::istream_iterator<char> as shown to copy from that to a string.

You can write data from a std::istream to a std::string with a std::istream_iterator . Sort of. The formatting can be wonky.

If you want to read data from a std::string using an iterator, its iterator type is std::string::iterator and an iterator to the front of the string is s.begin() , like other STL containers.

If you just have a bunch of data stored in memory that you want to send to both an output iterator and a string, you can use one of its other constructors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM