ostringstream ss;
ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << ss.str();
应打印以下内容:
(1,2)
(1,3)
(1,4)
我怎样才能按行反转输出以便打印:
(1,4)
(1,3)
(1,2)
ostringstream ss;
ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << ss.str();
应打印以下内容:
(1,2)
(1,3)
(1,4)
我怎样才能按行反转输出以便打印:
(1,4)
(1,3)
(1,2)
您可以使用std::streambuf
内部保留一堆std::string
s 并在使用str()
成员时将它们放在一起的自定义。例如:
#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();
}
对于实际应用程序,您显然应该在流缓冲区中设置一个缓冲区以提高性能。您可能还想创建一个直接初始化流的流缓冲区的自定义流。
您可以使用反向迭代器:
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"} );
此代码将需要:
#include <iostream> #include <algorithm> #include <iterator> #include <string> #include <sstream>
和基本的 C++11 支持。
将原始代码与 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;
}
这将打印:
(1,4)
(1,3)
(1,2)
如预期的
注意:这会从原始字符串中去除换行符。 而且,这需要:
//for `getline`:
#include <cstdlib>
//for `reverse`:
#include <algorithm>
//for `string`:
#include <string>
//for `vector`:
#include <vector>
这将是经典的方式,最好地利用标准 C++ 库。
#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;
}