Advertisement
zhangsongcui

StringFromFormat

Feb 23rd, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdarg>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #ifdef __GNUC__
  7. #   if defined _WIN32 && !defined __USE_MINGW_ANSI_STDIO
  8. __attribute__((__format__ (ms_printf, 1, 2)))
  9. #   else
  10. __attribute__((__format__ (printf, 1, 2)))
  11. #   endif
  12. #endif
  13. std::string StringFromFormat(const char *format, ...)
  14. {
  15.     std::va_list args;
  16.     va_start(args, format);
  17.     std::size_t num = static_cast<std::size_t>(vsnprintf(NULL, 0, format, args));
  18.     std::string result(num, '\0');
  19.     vsprintf(&result[0], format, args);
  20.     va_end(args);
  21.     return result;
  22. }
  23.  
  24. int main()
  25. {
  26.     using namespace std;
  27.     cout << StringFromFormat("%d+%d=%d23423423423423sdfgsdfgsdfgsdfg42342342343434234", 1, 2, 3);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement