Question

ostringstream ss;
ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << ss.str();

should print the following:

(1,2)

(1,3)

(1,4)

How could i reverse the output by line so that it prints:

(1,4)

(1,3)

(1,2)

Was it helpful?

Solution 2

Using your original code with C++98:

  ostringstream ss;
  ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
  cout << ss.str();

  //assign a string to the contents of the ostringstream:
  string rawlines = ss.str();

  //now create an input stringstream with the value of the rawlines
  istringstream iss(rawlines);

  string temp;//just a temporary object used for storage
  vector<string> lines;//this is where your lines will be held

  //now iterate over the stream and store the contents into the vector `lines`:
  while(getline(iss, temp)) {
      lines.push_back(temp);
  }

  //now reverse the contents:
  reverse(lines.begin(), lines.end());

  //see what's inside:
  for (vector<string>::const_iterator it = lines.begin(); it != lines.end(); ++it) {
    cout << *it << endl;
  }

This will print:

(1,4)
(1,3) 
(1,2)

As desired

NOTE: This strips the newlines from the the original string. And, this requires:

//for `getline`:
#include <cstdlib>
//for `reverse`:
#include <algorithm>
//for `string`:
#include <string>
//for `vector`:
#include <vector>

OTHER TIPS

You could use a custom std::streambuf which internally keeps a stack of std::strings and puts them together upon using a str() member. For example:

#include <iostream>
#include <numeric>
#include <streambuf>
#include <string>
#include <vector>

class stackbuf
    : public std::streambuf
{
    std::vector<std::string> d_lines;
    int overflow(int c) {
        if (c != std::char_traits<char>::eof()) {
            this->d_lines.back().push_back(c);
            if (c == '\n') {
                this->d_lines.push_back(std::string());
            }
        }
        return std::char_traits<char>::not_eof(c);
    }
public:
    stackbuf(): d_lines(1) {}
    std::string str() const {
        return std::accumulate(this->d_lines.rbegin(),
                               this->d_lines.rend(),
                               std::string());
    }
};

int main()
{
    stackbuf sbuf;
    std::ostream out(&sbuf);
    out << "(1, 2)\n(1, 3)\n(1, 4)\n";
    std::cout << sbuf.str();
}

For a real-world application you should, obviously, set up a buffer in the stream buffer to improve the performance. You may also want to create a custom stream directly initializing the stream's stream buffer.

You can use reverse iterators:

std::ostringstream ss{ "(1,2)\n(1,3)\n(1,4)\n" };
std::string str = ss.str();

std::copy( str.rbegin(), str.rend(),
           std::ostream_iterator<std::string>{std::cout, "\n"} );

This code will require:

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

and basic C++11 support.

This would be the classic way, best leveraging the standard C++ library.

#include <iostream>                                                      
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main(int argv, char* arv[])
{
  ostringstream oss;
  oss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
  cout << oss.str() << "----------\n";
  // Reverse lines
  // Fill an istringstream with buffer contents of the ostringstream
  istringstream iss(oss.str());
  stack<string> stk;
  while (iss) {
    string s;
    if (!getline(iss, s)) break; // Read a line
    s += '\n';                   // Put back newline stripped by readline
    stk.push(s);                 // Push line to stack
  }
  oss.clear();                   // Clear state of the ostringstream
  oss.str("");                   // Clear contents of the ostringstream for reuse
  while (!stk.empty()) {
    string s;
    s = stk.top();               // Get top of stack
    oss << s;                    // Output it to the ostringstream
    stk.pop();                   // Pop and throw away top of stack
  }
  cout << oss.str();
  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top