I'm wondering if I can use std::replace to replace the double quotes in a stringstream with single quotes.
I have:
std::replace(
std::ostreambuf_iterator<char>(ssScript),
std::ostreambuf_iterator<char>(),
'"', '\''
);
But of course ostreambuf_iterator has no default constructor, so this won't compile.
Is there another way to replace occurrences of a char in a stringstream inline like this?
The std::stringstream
class provides an interface for manipulating the stream, not its content. To manipulate the content of the stream you'd have to get the string, manipulate it and then put the string in the stream like this:
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
int main(void)
{
std::stringstream ss;
ss << "\"this is a string in a stream\"";
std::cout << "Before: " << ss.str() << std::endl;
std::string s = ss.str();
std::replace(s.begin(), s.end(), '"', '\'');
ss.str(s);
std::cout << "After: " << ss.str() << std::endl;
return 0;
}
And you get:
Before: "this is a string in a stream"
After: 'this is a string in a stream'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With