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();
Yes, you are likely to see the address of the stringstream. If you want to display the string it contains, try cout << stream.str();
setw is going to set the width of the entire formatted output, including the displayed base, which is why you’re not seeing the leading 0. Also, there’s no way to make the base be displayed in lowercase if you use std::showbase along with std::uppercase. The solution is to insert the base manually, and then apply … Read more
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
#include <sstream> #include <iomanip> std::stringstream ss; ss << std::hex << std::setfill(‘0’); for (int i = 0; i < 32; ++i) { ss << std::setw(2) << static_cast<unsigned>(buffer[i]); }
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 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
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
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
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
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