zoukankan      html  css  js  c++  java
  • 不定参数的格式化

    #include<cstdarg>
    #include<cstdio>
    std::string formatt(const char * format,...)
    {
        va_list args;
        (void)va_start(args,format);

        std::string retstr;

        size_t size = 1024;
        while(size <= 10*1024*1024)
        {
            char * buf = new char[size];
            memset(buf,0,size);

            int ret = vsnprintf(buf,size,(const char *)format,args);
            if(ret <= -1 || ret >=  (int)size)
            {
                size = size<<1;
                delete[] buf;
                continue;
            }

            retstr.assign(buf);
            delete[] buf;
            break;
        }

        va_end(args);

        return retstr;
    }


    int main()
    {
        int id  = 12;
        std::string cc = "myname is xiaohan";
        std::string str_cc = formatt(" msg : %s. myid is %d ,",cc.c_str(),id);
        std::cout << str_cc << std::endl;
        return 0;
    }

  • 相关阅读:
    Excel Sheet Column Number
    HappyNum
    isIsomorphic
    Contains DuplicateII
    iis7 设置http 自动跳转到https
    php 安装redis
    java 打包 war包
    NPOI 操作excel之 将图片插入到指定位置;
    nopi 简洁笔记
    vs11 微软下载地址
  • 原文地址:https://www.cnblogs.com/haibianxiaolu/p/3880703.html
Copyright © 2011-2022 走看看