cout

Yes, you are likely to see the address of the stringstream. If you want to display the string it contains, try cout << stream.str();

Should I preallocate std::stringstream?

Have you profiled your execution, and found them to be a source of slow down? Consider their usage. Are they mostly for error messages outside the normal flow of your code? As far as reserving space… Some implementations probably reserve a small buffer before any allocation takes place for the stringstream. Many implementations of std::string … Read more

C++ Extract number from the middle of a string

You can also use the built in find_first_of and find_first_not_of to find the first “numberstring” in any string. std::string first_numberstring(std::string const & str) { char const* digits = “0123456789”; std::size_t const n = str.find_first_of(digits); if (n != std::string::npos) { std::size_t const m = str.find_first_not_of(digits, n); return str.substr(n, m != std::string::npos ? m-n : m); } … Read more

How to deal with last comma, when making comma separated string? [duplicate]

How about this: #include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <string> #include <sstream> int main() { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); std::ostringstream ss; if(!v.empty()) { std::copy(v.begin(), std::prev(v.end()), std::ostream_iterator<int>(ss, “, “)); ss << v.back(); } std::cout << ss.str() << “\n”; } No need to add extra variables and doesn’t even depend on … Read more

remove char from stringstream and append some data

You can seek the stringstream and go back 1 character, using stringstream::seekp. Note that it does not remove the last character, but only moves the write head. This is sufficient in this case, as we overwrite the last character with an }. douCoh << ‘{‘; for(unsigned int i=0;i<dataSize;i++) if(v[i].test) douCoh << i+1 << ‘,’; douCoh.seekp(-1,douCoh.cur); … Read more

How to initialize a std::stringstream?

stringstream ss << “Number of people is ” << numPeople; Why can’t I assign the stringstream value at the same time I declare it? This is similar to hoping this would work… int x + 3 + 9; …but this doesn’t parse as a variable definition, let alone a definition and assignment. The legal way … Read more

How to test whether stringstream operator>> has parsed a bad type and skip it

The following code works well to skip the bad word and collect the valid double values istringstream iss(“2.832 1.3067 nana 1.678”); double num = 0; while(iss >> num || !iss.eof()) { if(iss.fail()) { iss.clear(); string dummy; iss >> dummy; continue; } cout << num << endl; } Here’s a fully working sample. Your sample almost … Read more

When should I use string instead of stringstream?

I don’t know which one will be faster, but if I had to guess I’d say your second example is, especially since you’ve called the reserve member function to allocate a large space for expansion. If you’re only concatenating strings use string::append (or string::operator+=). If you’re going to convert numbers to their string representation, as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)