Jump to content
  • Advertisement
Sign in to follow this  

std::stringstream, reuse of

This topic is 3763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my code i have a whole bunch of the following uses of std::stringstream to convert data to stings and then send the string data, so some particular function that requires a string or C-string. Such as the following, every frame, a new std::stringstream is created, the precision set, some data passed into it, and then the Cstring pointer is sent to another function, and this std::stringstream is lost when the scope ends. But this is done every frame, and is done a bunch of times for all the other win32 dialog edit boxes that i fill with data, so im wondering which is better, the first example or the second, by where in the second, the same std::stringstream, is used, its just cleared.
				std::stringstream MouseXPos2;
				static std::string  LastMouseXPos2;
				MouseXPos2 << std::fixed << MousePosition.x;
				if(MouseXPos2.str() != LastMouseXPos2){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_X_PIXELS), WM_SETTEXT, 0, (LPARAM) MouseXPos2.str().c_str());
					LastMouseXPos2 = MouseXPos2.str();

				std::stringstream MouseYPos2;
				static std::string  LastMouseYPos2;
				MouseYPos2 <<  std::fixed << MousePosition.y;
				if(MouseYPos2.str() != LastMouseYPos2){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_Y_PIXELS), WM_SETTEXT, 0, (LPARAM) MouseYPos2.str().c_str());
					LastMouseYPos2 = MouseYPos2.str();
std::stringstream MousePos;
				static std::string  LastMouseXPos, LastMouseYPos;
				MousePos << std::fixed << MousePosition.x;
				if(MousePos.str() != LastMouseXPos){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_X_PIXELS), WM_SETTEXT, 0, (LPARAM) MousePos.str().c_str());
					LastMouseXPos = MousePos.str();

				MousePos <<  std::fixed << MousePosition.y;
				if(MousePos.str() != LastMouseYPos){
					SendMessage(GetDlgItem(HwndSide,IDC_EDIT_GRID_POINTER_Y_PIXELS), WM_SETTEXT, 0, (LPARAM) MousePos.str().c_str());
					LastMouseYPos = MousePos.str();

Share this post

Link to post
Share on other sites
The second example sounds better, however if this is in performance critical code then i seriously suggest not using std::stringstream. We did some benchmarks and stringstream is signficantly slower than alternatives such as atoi() and variants and also snsprintf() and variants. std::stringstream does a hell of a lot of stuff under the hood and in situations where you know exactly how something is going to be converted and you don't need a cater-for-all-data-types solution, use something other than this.

If you use any of the functions mentioned, make sure you use the safe versions (to which you specify the length of the destination buffer).

Hope that helps,

Share this post

Link to post
Share on other sites
Ugh. Don't repeat code. Write functions.

// Of course, I'm totally guessing at certain types; adjust as needed

std::string asString(double value) {
std::stringstream converter;
return (converter << std::fixed << value).str();

void sendText(int dlgIdForSpacing, const string& message) {
SendMessage(GetDlgItem(HwndSide, dlgIdForSpacing), WM_SETTEXT, 0, (LPARAM) message.c_str());

static std::string LastMouseXPos, LastMouseYPos;
LastMouseXPos = asString(MousePosition.x);
LastMouseYPos = asString(MousePosition.y);

Or were you talking about performance?

Please, don't use atoi() unless you fully understand its behaviour and are sure it's what you want. If it receives non-numeric data, it will just return 0. There is no way to determine, just from that result, whether the data was garbage or was actually the string "0".

But if you really care, try it a few different ways, and see how the performance is as a result. In release mode, of course. And make sure the output is correct, too.

Share this post

Link to post
Share on other sites
Yes, it was a discussion on performance.

I have since discovered a win32 message that actually takes an int, which is handy.


But im still using the stringstream for my floats,

Share this post

Link to post
Share on other sites
By that, i take it you mean either

1. Dialogue boxes are are slow by nature


2. Dialogue boxes don't need to be quick

Well, its actually a

directx game

, that im making with some extra debugging functionality.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!