stringstream

C++ Using stringstream after << as parameter

…衆ロ難τιáo~ 提交于 2020-01-21 03:15:08
问题 Is it possible to write a method that takes a stringstream and have it look something like this, void method(string str) void printStringStream( StringStream& ss) { method(ss.str()); } And can be called like this stringstream var; printStringStream( var << "Text" << intVar << "More text"<<floatvar); I looked up the << operator and it looks like it returns a ostream& object but I'm probably reading this wrong or just not implementing it right. Really all I want is a clean way to concatenate

Why stringstream has this behavior?

谁说胖子不能爱 提交于 2020-01-17 05:32:09
问题 I have a code like this, concerning stringstream. I found a strange behavior: #include <iostream> #include <fstream> #include <sstream> using namespace std; int main() { int p, q; fstream file; string str; stringstream sstr; file.open("file.txt", ios::in); if(file.is_open()) { while(getline(file, str)) { sstr << str; sstr >> p >> q; cout << p << ' ' << q << endl; sstr.str(""); } } file.close(); return 0; } Suppose I have file.txt as 4 5 0 2 with return after 5 in the first line and 2 in the

Stringstream to Vector<char> throws std::bad_alloc

北城以北 提交于 2020-01-17 02:00:12
问题 I was tinkering with Boost Property Tree a bit, and I came across this example. I needed to convert the final result to vector, so I just added one more line after write_json(ss, root); like this: std::vector<char> testVec(std::begin(ss.str()), std::end(ss.str())); I have also tried this instead: std::string someString = ss.str() char* someArray = (char*)someString.c_str(); std::vector<char> someVec(someArray, someArray+sizeof(someArray)); In both the cases, I am getting this error: terminate

Stringstream to Vector<char> throws std::bad_alloc

折月煮酒 提交于 2020-01-17 02:00:02
问题 I was tinkering with Boost Property Tree a bit, and I came across this example. I needed to convert the final result to vector, so I just added one more line after write_json(ss, root); like this: std::vector<char> testVec(std::begin(ss.str()), std::end(ss.str())); I have also tried this instead: std::string someString = ss.str() char* someArray = (char*)someString.c_str(); std::vector<char> someVec(someArray, someArray+sizeof(someArray)); In both the cases, I am getting this error: terminate

Custom class ostringstream output error

断了今生、忘了曾经 提交于 2020-01-15 10:54:30
问题 I don't know why this is erroring, but I'm just trying to add something "akin" to endl so that I can throw what's in an ostringstream to our debugger. I have the following: class debug_stream_info { public: debug_stream_info(int errorLine, char *errorFile, int level) :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level) { } friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info); private: int m_errorLine; std::string m

Custom class ostringstream output error

荒凉一梦 提交于 2020-01-15 10:54:09
问题 I don't know why this is erroring, but I'm just trying to add something "akin" to endl so that I can throw what's in an ostringstream to our debugger. I have the following: class debug_stream_info { public: debug_stream_info(int errorLine, char *errorFile, int level) :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level) { } friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info); private: int m_errorLine; std::string m

Stringstream error: cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

那年仲夏 提交于 2020-01-14 14:12:41
问题 In creating a simple exception class extension (where I can construct error messages more easily), I've isolated an error down to the following simple code: #include <sstream> #include <string> class myCout { public: std::stringstream ssOut; // Removing this gets rid of error template <typename T> myCout& operator << (const T &x) { // Do some formatting return *this; } }; class myErr : public myCout { public: using myCout::operator<<; }; int main(int argc, const char* argv[]) { throw myErr()

Check if std::stringstream contains a character - buffering until \n

不羁的心 提交于 2020-01-06 21:58:54
问题 Because I was unable to find how to output raw data in android debug output (eg. without \n automatically inserted), I decided to subclass our logging library and buffer the input until \n appears. Our logging library accepts huge number of data formats, so I decided to create a template method: template<typename T> bool addToLog(std::stringstream* stream, android_LogPriority priority, T data) { // Add new data to sstream stream<<data; //If the stream now contains new line if(stream->PSEUDO

C++在Windows下获取时间

我与影子孤独终老i 提交于 2020-01-06 17:06:29
文章目录 Windows下计算程序运行时间 Windows下返回系统时间 Windows下计算程序运行时间 Windows提供了获取CPU运算频率的函数 QueryPerformanceFrequency(LARGE_INTEGER*) 以及获取当前CPU周期数的函数 QueryPerformanceCounter(LARGE_INTEGER) 。所以更精确的时间计算方法就是求出CPU周期数的差值,除以运算频率,单位为秒。 LARGE_INTEGER freq = { 0 } ; LARGE_INTEGER start = { 0 } ; LARGE_INTEGER end = { 0 } ; QueryPerformanceFrequency ( & freq ) ; QueryPerformanceCounter ( & start ) ; Sleep ( 100 ) ; QueryPerformanceCounter ( & end ) ; std :: cout << "frequency: " << freq . QuadPart << std :: endl ; std :: cout << "end - start: " << ( end . QuadPart - start . QuadPart ) << std :: endl ; std :: cout << "

stringstream and str not synchronized

别等时光非礼了梦想. 提交于 2020-01-06 13:59:28
问题 I'm writing a simple parser in c++. I would like to remove leading whitespaces with std::ws . bool Parser::readWhiteSpace() { std::cout << "Before : str=[" << this->_ss.str() << "], peek=[" << (char)this->_ss.peek() << ']'<< std::endl; this->_ss >> std::ws; std::cout << "After : str=[" << this->_ss.str() << "], peek=[" << (char)this->_ss.peek() << ']'<< std::endl; return (true); } The output is : Before : str=[ something], peek=[ ] After : str=[ something], peek=[s] I don't understand why the