Created
April 15, 2016 03:54
-
-
Save waveacme/6dc6049f6dafab752afdbfa11b75c7d5 to your computer and use it in GitHub Desktop.
append int to string in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://stackoverflow.com/questions/191757/c-concatenate-string-and-int | |
std::string name = "John"; | |
int age = 21; | |
std::string result; | |
// 1. with Boost | |
result = name + boost::lexical_cast<std::string>(age); | |
// 2. with C++11 | |
result = name + std::to_string(age); | |
// 3. with FastFormat.Format | |
fastformat::fmt(result, "{0}{1}", name, age); | |
// 4. with FastFormat.Write | |
fastformat::write(result, name, age); | |
// 5. with IOStreams | |
std::stringstream sstm; | |
sstm << name << age; | |
result = sstm.str(); | |
// 6. with itoa | |
char numstr[21]; // enough to hold all numbers up to 64-bits | |
result = name + itoa(age, numstr, 10); | |
// 7. with sprintf | |
char numstr[21]; // enough to hold all numbers up to 64-bits | |
sprintf(numstr, "%d", age); | |
result = name + numstr; | |
// 8. with STLSoft's integer_to_string | |
char numstr[21]; // enough to hold all numbers up to 64-bits | |
result = name + stlsoft::integer_to_string(numstr, 21, age); | |
// 9. with STLSoft's winstl::int_to_string() | |
result = name + winstl::int_to_string(age); | |
// 10. With Poco NumberFormatter | |
result = name + Poco::NumberFormatter().format(age); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment