I want to add string and int

Asked 2 years ago, Updated 2 years ago, 110 views

I thought it would be easy, but it's hard ㅜㅜㅜ What should I do to make string name become "John21" in the code below?

string name = "John";
int age = 21;

c++ int concatenation stdstring

2022-09-22 22:36

1 Answers

I'll write it in alphabetical order.

// 1. Boost result = name + boost::lexical_cast<std::string>(age).

// 2. FastFormat.Format fastformat::fmt(result, "{0}{1}", name, age);

// 3. FastFormat.Write fastformat::write(result, name, age);

// 4. IOStreams std::stringstream sstm; sstm << name << age; result = sstm.str();

// 5. itoa char numstr[21]; // enough to hold all numbers up to 64-bits result = name + itoa(age, numstr, 10);

// 6. sprintf char numstr[21]; // enough to hold all numbers up to 64-bits sprintf(numstr, "%d", age); result = name + numstr;

// 7. 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);

// 8. STLSoft's winstl::int_to_string() result = name + winstl::int_to_string(age);

// 9. Poco NumberFormatter result = name + Poco::NumberFormatter().format(age);


2022-09-22 22:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.