#include <string> #include <sstream> #ifdef WIN32 #include <windows.h> #endif ///////////////////////////////////// // the 'to_string' function template< class type> inline std::string to_string( const type & value) { std::ostringstream streamOut; streamOut << value; return streamOut.str(); } struct Point { Point( int nLeft, int nTop) : m_nLeft( nLeft), m_nTop( nTop) {} int m_nLeft; int m_nTop; }; std::ostream & operator<<( std::ostream & streamOut, const Point & value) { streamOut << "(" << value.m_nLeft << ", " << value.m_nTop << ")"; return streamOut; } int main() { std::string strDocName = "doc_test.txt"; int nWordsCount; // ... calculate words count nWordsCount = 48; // {1} // using to_string with an 'int' std::string str1 = "The " + strDocName + " contains " + to_string( nWordsCount) + " words"; // on Win32, call the Message box function #ifdef WIN32 MessageBox( NULL, str1.c_str(), "", MB_OK); #endif // {2} // using to_string with a custom type (Point) std::string str2 = "You should build a rectangle between " + to_string( Point( 10, 5)) + " and " + to_string( Point( 100, 300)); // on Win32, call the Message box function #ifdef WIN32 MessageBox( NULL, str2.c_str(), "", MB_OK); #endif return 0; } |