C++ converting an int to a string -


i know extremely basic i'm new c++ , can't seem find answer. i'm trying convert few integers strings. method works:

int = 10; stringstream ss; ss << a; string str = ss.str(); 

but when need convert second , third ones this:

int b = 13; stringstream ss; ss << a; string str2 = ss.str();  int c = 15; stringstream ss; ss << b; string str3 = ss.str(); 

i error:

'std::stringstream ss' declared here 

do need somehow close stringstream? i've noticed if put them far away each other in code compiler doesn't mind doesn't seem should do. have suggestions?

you're trying redeclare same stringstream same name. can modify code in order work :

 int b = 13; stringstream ss2; ss2 << a; string str2 = ss2.str(); 

or if don't want redeclare :

int b = 13; ss.str(""); // empty stringstream ss.clear(); ss << a; string str2 = ss.str(); 

you can use , quicker :

int c = 42; std::string s = std::to_string(c); 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -