std::string formatting like sprintf

前端 未结 30 2617
野趣味
野趣味 2020-11-22 04:42

I have to format std::string with sprintf and send it into file stream. How can I do this?

30条回答
  •  青春惊慌失措
    2020-11-22 05:27

    I usually use this:

    std::string myformat(const char *const fmt, ...)
    {
            char *buffer = NULL;
            va_list ap;
    
            va_start(ap, fmt);
            (void)vasprintf(&buffer, fmt, ap);
            va_end(ap);
    
            std::string result = buffer;
            free(buffer);
    
            return result;
    }
    

    Disadvantage: not all systems support vasprint

提交回复
热议问题