Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare content of std::stringstream

I have two std::stringstream: ss1 and ss2, which are given as an argument to two different functions that fills them with data.

I want to sanity check that these functions writes the same content. How do I test for equality of two std::stringstream?

alg1.print(ss1);
alg2.print(ss2);

if(ss1 != ss2) {
   cout << "Content not identical!" << endl;
}

Does not work. Converting them both to str() works, however this seems ineffective. There must be a better way?

like image 203
YnkDK Avatar asked Mar 09 '26 15:03

YnkDK


1 Answers

I think the only way to do so is to use the .str() method:

if (ss1.str() != ss2.str()) {
  cout << "Content not identical!" << endl;
}

I'm not sure if this is particulary fast but except if you are in an embedded context or you are checking millions of comparisons per second it should not matter.

like image 99
gabry Avatar answered Mar 12 '26 06:03

gabry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!