In my code there is a loop that adds sth like that \"number,\" to stringstream. When it ends, I need to extract \',\' add \'}\' and add \'{\' if the loop is to repeated.
I've found this way using pop_back()
string's method since c++11. Probably not so good as smarter ones above, but useful in much more complicated cases and/or for lazy people.
douCoh << '{';
for(unsigned int i=0;i<dataSize;i++)
if(v[i].test) douCoh << i+1 << ',';
string foo(douCoh.str());
foo.pop_back();
douCoh.str(foo);
douCoh.seekp (0, douCoh.end);
douCoh << '}';
You could use std::string::erase to remove the last character directly from the underlying string.
Why not just check the counter? And not insert the ','
douCoh << '{';
for(unsigned int i=0;i<dataSize;i++){
if(v[i].test){
douCoh << i+1;
if(i != dataSize - 1) douCoh << ',';
}
}
/*douCoh.get();*/ douCoh << '}';