开发者

ostringstream problem with int in c++

开发者 https://www.devze.com 2022-12-13 03:59 出处:网络
I would expect the following code to output hello5. Instead, it only outputs hello. It seems to be a problem with trying to output an int to the ostringstream.

I would expect the following code to output hello5. Instead, it only outputs hello. It seems to be a problem with trying to output an int to the ostringstream. When I output the same directly to cout I receive the expected input. Using XCode 3.2 on Snow Leopard.

Thanks!

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
 int myint = 5;
 string mystr = "hell开发者_开发百科o";
 string finalstr;
 ostringstream oss;

 oss << mystr << myint;
 finalstr = oss.str();

 cout << finalstr;


 return 0;
}

EDIT: See the answer I posted below. This seems to be created by a problem in the Active Configuration 'Debug' in XCode 3.2 on Snow Leopard


Changing the Active Configuration in XCode from 'Debug' to 'Release' works as a workaround.


Your code is correct, it writes hello5 on my Windows 7 machine. Maybe the problem is rather that you don't write a std::endl or something which might confuse your OS.


Yep, tested on this end (windows XP Pro) and it works swimmingly


I just tested and it worked just fine on my Mac with Xcode 3.2.1 and Snow Leopard. It not that your prompt is shadowing the output? Try add an endl to the cout line?

-- Edit --

My test suite

  • c++ test.cpp -- works fine
  • c++ -D_GLICXX_DEBUG=1 test.cpp -- fail
  • c++ -arch i386 -D_GLICXX_DEBUG=1 test.cpp -- works fine

What can we say about this? In short, Debug version of 64 bit stdc++ seem to be broken.

0

精彩评论

暂无评论...
验证码 换一张
取 消