code_junkie

Alternative to itoa() for converting integer to string C++?

Nov 11th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <sstream>
  2.  
  3. int i = 5;
  4. std::string s;
  5. std::stringstream out;
  6. out << i;
  7. s = out.str();
  8.  
  9. #include <boost/lexical_cast.hpp>
  10. int main(int argc, char** argv) {
  11. std::string foo = boost::lexical_cast<std::string>(argc);
  12. }
  13.  
  14. char str[12];
  15. int num = 3;
  16. sprintf(str, "%d", num); // str now contains "3"
  17.  
  18. snprintf(str, sizeof(str), "%d", num);
  19.  
  20. std::stringstream str;
  21. str << myint;
  22. std::string result;
  23. str >> result;
  24.  
  25. int i = 10;
  26. std::string result;
  27.  
  28. result = str(boost::format("%1%", i));
  29.  
  30. fastformat::fmt(result, "{0}", i);
  31. fastformat::write(result, i);
  32.  
  33. //Mark Renslow, Globe University, Minnesota School of Business, Utah Career College
  34. //C++ instructor and Network Dean of Information Technology
  35.  
  36. #include <cstdlib>
  37. #include <iostream>
  38. #include <string>
  39. #include <sstream> // string stream
  40. #include <direct.h>
  41.  
  42. using namespace std;
  43.  
  44. string intToString(int x)
  45. {
  46. /**************************************/
  47. /* This function is similar to itoa() */
  48. /* "integer to alpha", a non-standard */
  49. /* C language function. It takes an */
  50. /* integer as input and as output, */
  51. /* returns a C++ string. */
  52. /* itoa() returned a C-string (null- */
  53. /* terminated) */
  54. /* This function is not needed because*/
  55. /* the following template function */
  56. /* does it all */
  57. /**************************************/
  58. string r;
  59. stringstream s;
  60.  
  61. s << x;
  62. r = s.str();
  63.  
  64. return r;
  65.  
  66. }
  67.  
  68. template <class T>
  69. string toString( T argument)
  70. {
  71. /**************************************/
  72. /* This template shows the power of */
  73. /* C++ templates. This function will */
  74. /* convert anything to a string! */
  75. /* Precondition: */
  76. /* operator<< is defined for type T */
  77. /**************************************/
  78. string r;
  79. stringstream s;
  80.  
  81. s << argument;
  82. r = s.str();
  83.  
  84. return r;
  85.  
  86. }
  87.  
  88. int main( )
  89. {
  90. string s;
  91.  
  92. cout << "What directory would you like me to make?";
  93.  
  94. cin >> s;
  95.  
  96. try
  97. {
  98. mkdir(s.c_str());
  99. }
  100. catch (exception& e)
  101. {
  102. cerr << e.what( ) << endl;
  103. }
  104.  
  105. chdir(s.c_str());
  106.  
  107. //Using a loop and string concatenation to make several sub-directories
  108. for(int i = 0; i < 10; i++)
  109. {
  110. s = "Dir_";
  111. s = s + toString(i);
  112. mkdir(s.c_str());
  113. }
  114. system("PAUSE");
  115. return EXIT_SUCCESS;
  116. }
  117.  
  118. int number = 123;
  119.  
  120. stringstream = s;
  121.  
  122. s << number;
  123.  
  124. cout << ss.str() << endl;
  125.  
  126. template <typename T> string toStr(T tmp)
  127. {
  128. ostringstream out;
  129. out << tmp;
  130. return out.str();
  131. }
  132.  
  133.  
  134. template <typename T> T strTo(string tmp)
  135. {
  136. T output;
  137. istringstream in(tmp);
  138. in >> output;
  139. return output;
  140. }
Add Comment
Please, Sign In to add comment