Skip to content

Instantly share code, notes, and snippets.

@TheRolfFR
Created October 20, 2022 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheRolfFR/f07155f06d899ec8c71b79ec6d875bdf to your computer and use it in GitHub Desktop.
Save TheRolfFR/f07155f06d899ec8c71b79ec6d875bdf to your computer and use it in GitHub Desktop.
C++ convert int to string
std::string from_int(int value) {
int tmp = value;
std::string valueAsString = tmp < 0 ? "-" : "";
size_t pos = valueAsString.length();
if(tmp < 0) tmp = -tmp;
else if(tmp == 0) valueAsString = "0";
while(tmp != 0) {
int digit = tmp%10;
valueAsString.insert(pos,1,'0' + digit);
tmp/=10;
}
return valueAsString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment