Ist es möglich, den Inhalt eines std::ostringstream
ohne die std::ostringstream::str()
Mitgliedsfunktion zu suchen, um eine std::string
Suche zu erstellen?Abfrage std :: ostringstream Inhalt
Ich habe folgende und eine std::string
Instanz bei jedem Aufruf von flush_()
Konstruktion vermeiden will:
#include <iostream>
using std::cout;
#include <ios>
using std::boolalpha;
#include <sstream>
using std::ostringstream;
#include <string>
using std::string;
class line_decorating_ostream
{
public:
line_decorating_ostream() { out_ << boolalpha; }
~line_decorating_ostream() { cout << out_.str(); }
template <typename T>
line_decorating_ostream& operator<<(const T& a_t)
{
out_ << a_t;
flush_();
return *this;
}
private:
ostringstream out_;
line_decorating_ostream(const line_decorating_ostream&);
line_decorating_ostream& operator=(const line_decorating_ostream&);
// Write any full lines.
void flush_()
{
string s(out_.str());
size_t pos = s.find('\n');
if (string::npos != pos)
{
do
{
cout << "line: [" << s.substr(0, pos) << "]\n";
s = s.substr(pos + 1);
} while (string::npos != (pos = s.find('\n')));
out_.clear();
out_.str("");
out_ << boolalpha << s;
}
}
};
int main()
{
line_decorating_ostream logger;
logger << "1 " << "2 " << 3 << " 4 " << 5 << "\n"
<< "6 7 8 9 10\n...\n" << true << "\n";
return 0;
}
[Es keine Performance-Probleme verursacht, dass ich besorgt bin, nur neugierig, ob dies möglich ist, .]
+1 Sie danken. Just beachten ich 'dynamic_cast hatte (str.rdbuf())' erhalten Zugang zu 'pbase' und' pptr'. –
hmjd