Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string name = "John";
- int age = 21;
- 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 the {fmt} library
- result = fmt::format("{}{}", name, age);
- // 6. with IOStreams
- std::stringstream sstm;
- sstm << name << age;
- result = sstm.str();
- // 7. with itoa
- char numstr[21]; // enough to hold all numbers up to 64-bits
- result = name + itoa(age, numstr, 10);
- // 8. with sprintf
- char numstr[21]; // enough to hold all numbers up to 64-bits
- sprintf(numstr, "%d", age);
- result = name + numstr;
- // 9. 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);
- // 10. with STLSoft's winstl::int_to_string()
- result = name + winstl::int_to_string(age);
- // 11. With Poco NumberFormatter
- result = name + Poco::NumberFormatter().format(age);
- auto result = name + std::to_string( age );
- std::stringstream ss;
- ss << age;
- std::cout << name << ss.str() << std::endl;
- char buffer[128];
- snprintf(buffer, sizeof(buffer), "%s%d", name.c_str(), age);
- std::cout << buffer << std::endl;
- #include <iostream>
- #include <sstream>
- std::ostringstream o;
- o << name << age;
- std::cout << o.str();
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- string itos(int i) // convert int to string
- {
- stringstream s;
- s << i;
- return s.str();
- }
- string s = name + std::to_string(age);
- std::string name = "John";
- int age = 21;
- name += std::to_string(age);
- std::cout << name;
- John21
- sprintf(outString,"%s%d",name,age);
- #include <string>
- #include <sstream>
- using namespace std;
- string concatenate(std::string const& name, int i)
- {
- stringstream s;
- s << name << i;
- return s.str();
- }
- #include <sstream>
- template <class T>
- inline std::string to_string (const T& t)
- {
- std::stringstream ss;
- ss << t;
- return ss.str();
- }
- std::string szName = "John";
- int numAge = 23;
- szName += to_string<int>(numAge);
- cout << szName << endl;
- template <typename L, typename R> std::string operator+(L left, R right) {
- std::ostringstream os;
- os << left << right;
- return os.str();
- }
- std::string foo("the answer is ");
- int i = 42;
- std::string bar(foo + i);
- std::cout << bar << std::endl;
- the answer is 42
- #include <string>
- #include <sstream>
- #include <bits/stdc++.h>
- #include <iostream>
- using namespace std;
- int main() {
- string name = "John";
- int age = 21;
- string answer1 = "";
- // Method 1). string s1 = to_string(age).
- string s1=to_string(age); // Know the integer get converted into string
- // where as we know that concatenation can easily be done using '+' in C++
- answer1 = name + s1;
- cout << answer1 << endl;
- // Method 2). Using string streams
- ostringstream s2;
- s2 << age;
- string s3 = s2.str(); // The str() function will convert a number into a string
- string answer2 = ""; // For concatenation of strings.
- answer2 = name + s3;
- cout << answer2 << endl;
- return 0;
- }
- CString nameAge = "";
- nameAge.Format("%s%d", "John", 21);
- #include <sstream>
- #define MAKE_STRING(tokens) /****************/
- static_cast<std::ostringstream&>(
- std::ostringstream().flush() << tokens
- ).str()
- /**/
- int main() {
- int i = 123;
- std::string message = MAKE_STRING("i = " << i);
- std::cout << message << std::endl; // prints: "i = 123"
- }
- QString string = QString("Some string %1 with an int somewhere").arg(someIntVariable);
- string.append(someOtherIntVariable);
- #include <boost/format.hpp>
- #include <string>
- int main()
- {
- using boost::format;
- int age = 22;
- std::string str_age = str(format("age is %1%") % age);
- }
- #include <boost/spirit/include/karma.hpp>
- #include <iterator>
- #include <string>
- int main()
- {
- using namespace boost::spirit;
- int age = 22;
- std::string str_age("age is ");
- std::back_insert_iterator<std::string> sink(str_age);
- karma::generate(sink, int_, age);
- return 0;
- }
- #include <iostream>
- #include <locale>
- #include <string>
- template <class Facet>
- struct erasable_facet : Facet
- {
- erasable_facet() : Facet(1) { }
- ~erasable_facet() { }
- };
- void append_int(std::string& s, int n)
- {
- erasable_facet<std::num_put<char,
- std::back_insert_iterator<std::string>>> facet;
- std::ios str(nullptr);
- facet.put(std::back_inserter(s), str,
- str.fill(), static_cast<unsigned long>(n));
- }
- int main()
- {
- std::string str = "ID: ";
- int id = 123;
- append_int(str, id);
- std::cout << str; // ID: 123
- }
- #include <sstream>
- std::ostringstream s;
- s << "John " << age;
- std::string query(s.str());
- std::string query("John " + std::to_string(age));
- #include <boost/lexical_cast.hpp>
- std::string query("John " + boost::lexical_cast<std::string>(age));
- char intToChar(int num)
- {
- if (num < 10 && num >= 0)
- {
- return num + 48;
- //48 is the number that we add to an integer number to have its character equivalent (see the unsigned ASCII table)
- }
- else
- {
- return '*';
- }
- }
- string intToString(int num)
- {
- int digits = 0, process, single;
- string numString;
- process = num;
- // The following process the number of digits in num
- while (process != 0)
- {
- single = process % 10; // 'single' now holds the rightmost portion of the int
- process = (process - single)/10;
- // Take out the rightmost number of the int (it's a zero in this portion of the int), then divide it by 10
- // The above combination eliminates the rightmost portion of the int
- digits ++;
- }
- process = num;
- // Fill the numString with '*' times digits
- for (int i = 0; i < digits; i++)
- {
- numString += '*';
- }
- for (int i = digits-1; i >= 0; i--)
- {
- single = process % 10;
- numString[i] = intToChar ( single);
- process = (process - single) / 10;
- }
- return numString;
- }
- string name = "John";
- int age = 5;
- char temp = 5 + '0';
- name = name + temp;
- cout << name << endl;
- Output: John5
- auto result = fmt::format("{}{}", name, age);
- auto result = std::format("{}{}", name, age);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement