Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. std::string name = "John";
  2. int age = 21;
  3.  
  4. std::string name = "John";
  5. int age = 21;
  6. std::string result;
  7.  
  8. // 1. with Boost
  9. result = name + boost::lexical_cast<std::string>(age);
  10.  
  11. // 2. with C++11
  12. result = name + std::to_string(age);
  13.  
  14. // 3. with FastFormat.Format
  15. fastformat::fmt(result, "{0}{1}", name, age);
  16.  
  17. // 4. with FastFormat.Write
  18. fastformat::write(result, name, age);
  19.  
  20. // 5. with the {fmt} library
  21. result = fmt::format("{}{}", name, age);
  22.  
  23. // 6. with IOStreams
  24. std::stringstream sstm;
  25. sstm << name << age;
  26. result = sstm.str();
  27.  
  28. // 7. with itoa
  29. char numstr[21]; // enough to hold all numbers up to 64-bits
  30. result = name + itoa(age, numstr, 10);
  31.  
  32. // 8. with sprintf
  33. char numstr[21]; // enough to hold all numbers up to 64-bits
  34. sprintf(numstr, "%d", age);
  35. result = name + numstr;
  36.  
  37. // 9. with STLSoft's integer_to_string
  38. char numstr[21]; // enough to hold all numbers up to 64-bits
  39. result = name + stlsoft::integer_to_string(numstr, 21, age);
  40.  
  41. // 10. with STLSoft's winstl::int_to_string()
  42. result = name + winstl::int_to_string(age);
  43.  
  44. // 11. With Poco NumberFormatter
  45. result = name + Poco::NumberFormatter().format(age);
  46.  
  47. auto result = name + std::to_string( age );
  48.  
  49. std::stringstream ss;
  50. ss << age;
  51. std::cout << name << ss.str() << std::endl;
  52.  
  53. char buffer[128];
  54. snprintf(buffer, sizeof(buffer), "%s%d", name.c_str(), age);
  55. std::cout << buffer << std::endl;
  56.  
  57. #include <iostream>
  58. #include <sstream>
  59.  
  60. std::ostringstream o;
  61. o << name << age;
  62. std::cout << o.str();
  63.  
  64. #include <iostream>
  65. #include <string>
  66. #include <sstream>
  67. using namespace std;
  68. string itos(int i) // convert int to string
  69. {
  70. stringstream s;
  71. s << i;
  72. return s.str();
  73. }
  74.  
  75. string s = name + std::to_string(age);
  76.  
  77. std::string name = "John";
  78. int age = 21;
  79.  
  80. name += std::to_string(age);
  81.  
  82. std::cout << name;
  83.  
  84. John21
  85.  
  86. sprintf(outString,"%s%d",name,age);
  87.  
  88. #include <string>
  89. #include <sstream>
  90. using namespace std;
  91. string concatenate(std::string const& name, int i)
  92. {
  93. stringstream s;
  94. s << name << i;
  95. return s.str();
  96. }
  97.  
  98. #include <sstream>
  99.  
  100. template <class T>
  101. inline std::string to_string (const T& t)
  102. {
  103. std::stringstream ss;
  104. ss << t;
  105. return ss.str();
  106. }
  107.  
  108. std::string szName = "John";
  109. int numAge = 23;
  110. szName += to_string<int>(numAge);
  111. cout << szName << endl;
  112.  
  113. template <typename L, typename R> std::string operator+(L left, R right) {
  114. std::ostringstream os;
  115. os << left << right;
  116. return os.str();
  117. }
  118.  
  119. std::string foo("the answer is ");
  120. int i = 42;
  121. std::string bar(foo + i);
  122. std::cout << bar << std::endl;
  123.  
  124. the answer is 42
  125.  
  126. #include <string>
  127. #include <sstream>
  128. #include <bits/stdc++.h>
  129. #include <iostream>
  130. using namespace std;
  131.  
  132. int main() {
  133. string name = "John";
  134. int age = 21;
  135.  
  136. string answer1 = "";
  137. // Method 1). string s1 = to_string(age).
  138.  
  139. string s1=to_string(age); // Know the integer get converted into string
  140. // where as we know that concatenation can easily be done using '+' in C++
  141.  
  142. answer1 = name + s1;
  143.  
  144. cout << answer1 << endl;
  145.  
  146. // Method 2). Using string streams
  147.  
  148. ostringstream s2;
  149.  
  150. s2 << age;
  151.  
  152. string s3 = s2.str(); // The str() function will convert a number into a string
  153.  
  154. string answer2 = ""; // For concatenation of strings.
  155.  
  156. answer2 = name + s3;
  157.  
  158. cout << answer2 << endl;
  159.  
  160. return 0;
  161. }
  162.  
  163. CString nameAge = "";
  164. nameAge.Format("%s%d", "John", 21);
  165.  
  166. #include <sstream>
  167. #define MAKE_STRING(tokens) /****************/
  168. static_cast<std::ostringstream&>(
  169. std::ostringstream().flush() << tokens
  170. ).str()
  171. /**/
  172.  
  173. int main() {
  174. int i = 123;
  175. std::string message = MAKE_STRING("i = " << i);
  176. std::cout << message << std::endl; // prints: "i = 123"
  177. }
  178.  
  179. QString string = QString("Some string %1 with an int somewhere").arg(someIntVariable);
  180. string.append(someOtherIntVariable);
  181.  
  182. #include <boost/format.hpp>
  183. #include <string>
  184. int main()
  185. {
  186. using boost::format;
  187.  
  188. int age = 22;
  189. std::string str_age = str(format("age is %1%") % age);
  190. }
  191.  
  192. #include <boost/spirit/include/karma.hpp>
  193. #include <iterator>
  194. #include <string>
  195. int main()
  196. {
  197. using namespace boost::spirit;
  198.  
  199. int age = 22;
  200. std::string str_age("age is ");
  201. std::back_insert_iterator<std::string> sink(str_age);
  202. karma::generate(sink, int_, age);
  203.  
  204. return 0;
  205. }
  206.  
  207. #include <iostream>
  208. #include <locale>
  209. #include <string>
  210.  
  211. template <class Facet>
  212. struct erasable_facet : Facet
  213. {
  214. erasable_facet() : Facet(1) { }
  215. ~erasable_facet() { }
  216. };
  217.  
  218. void append_int(std::string& s, int n)
  219. {
  220. erasable_facet<std::num_put<char,
  221. std::back_insert_iterator<std::string>>> facet;
  222. std::ios str(nullptr);
  223.  
  224. facet.put(std::back_inserter(s), str,
  225. str.fill(), static_cast<unsigned long>(n));
  226. }
  227.  
  228. int main()
  229. {
  230. std::string str = "ID: ";
  231. int id = 123;
  232.  
  233. append_int(str, id);
  234.  
  235. std::cout << str; // ID: 123
  236. }
  237.  
  238. #include <sstream>
  239.  
  240. std::ostringstream s;
  241. s << "John " << age;
  242. std::string query(s.str());
  243.  
  244. std::string query("John " + std::to_string(age));
  245.  
  246. #include <boost/lexical_cast.hpp>
  247.  
  248. std::string query("John " + boost::lexical_cast<std::string>(age));
  249.  
  250. char intToChar(int num)
  251. {
  252. if (num < 10 && num >= 0)
  253. {
  254. return num + 48;
  255. //48 is the number that we add to an integer number to have its character equivalent (see the unsigned ASCII table)
  256. }
  257. else
  258. {
  259. return '*';
  260. }
  261. }
  262.  
  263. string intToString(int num)
  264. {
  265. int digits = 0, process, single;
  266. string numString;
  267. process = num;
  268.  
  269. // The following process the number of digits in num
  270. while (process != 0)
  271. {
  272. single = process % 10; // 'single' now holds the rightmost portion of the int
  273. process = (process - single)/10;
  274. // Take out the rightmost number of the int (it's a zero in this portion of the int), then divide it by 10
  275. // The above combination eliminates the rightmost portion of the int
  276. digits ++;
  277. }
  278.  
  279. process = num;
  280.  
  281. // Fill the numString with '*' times digits
  282. for (int i = 0; i < digits; i++)
  283. {
  284. numString += '*';
  285. }
  286.  
  287.  
  288. for (int i = digits-1; i >= 0; i--)
  289. {
  290. single = process % 10;
  291. numString[i] = intToChar ( single);
  292. process = (process - single) / 10;
  293. }
  294.  
  295. return numString;
  296. }
  297.  
  298. string name = "John";
  299. int age = 5;
  300. char temp = 5 + '0';
  301. name = name + temp;
  302. cout << name << endl;
  303.  
  304. Output: John5
  305.  
  306. auto result = fmt::format("{}{}", name, age);
  307.  
  308. auto result = std::format("{}{}", name, age);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement