[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppIntToStr
(function taken from from Marshall Cline's C++ FAQ Lite document http://www.parashift.com/c++-faq-lite/)
You could also use the Boost C++ Library's lexical_cast:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppIntToStr
(C++) Converting an integer to a std::string
In analogy with the standard 'atoi' function, doing the other way around:(function taken from from Marshall Cline's C++ FAQ Lite document http://www.parashift.com/c++-faq-lite/)
std::string itoa(const int x) { std::ostringstream o; if (!(o << x)) return "ERROR"; return o.str(); } int main() { assert(itoa(69)=="69"); }
- include <sstream>
- include <cassert>
You could also use the Boost C++ Library's lexical_cast:
int main() { const int i = 123; const std::string s = boost::lexical_cast<std::string>(i); assert(s == "123"); }
- include <cassert>
- include <string>
- include <boost/lexical_cast.hpp>
Other code snippets
(C++ Builder) Converting an integer to a String
Code links
- ::, scope operator
- #include
- assert
- char
- const
- if
- #include
- int
- main
- return
- ?sstream
- std
- std::string
- string
- scope operator, ::
- return
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
