File indexing completed on 2026-04-09 07:49:23
0001
0002
0003 #include <iostream>
0004 #include <cstdio>
0005 #include <cassert>
0006 #include <vector>
0007 #include <string>
0008
0009 template<typename ... Args>
0010 std::string Format( const char* fmt, Args ... args )
0011 {
0012 int sz = std::snprintf( nullptr, 0, fmt, args ... ) + 1;
0013 assert( sz > 0 );
0014 std::vector<char> buf(sz) ;
0015 std::snprintf( buf.data(), sz, fmt, args ... );
0016 return std::string( buf.begin(), buf.begin() + sz - 1 );
0017 }
0018
0019 int main(int argc, char** argv)
0020 {
0021 const char* fmt = " Hello %d World %4.2f \n" ;
0022 std::string s = Format(fmt, 101, 50.5f );
0023 std::cout << "[" << s << "]" << std::endl ;
0024 return 0 ;
0025 }
0026
0027