为什么要单独做C++字符串格式化?

C++中可以使用stringstream来格式化字符串如下:

stringstream sstream;
sstream << "I have made " << 500 << " dollars on this product.";
string formated_str = sstream.str();

但是,这种方式并不好用。有没有类似printf操作的方式呢?在opencv中有类似的封装。

怎么做?

怎么用?

string formated_str = format("I have made %d dollars on this product.", 500);

怎么封装的?

typedef std::string String;int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
{
#if defined _MSC_VERif (len <= 0) return len == 0 ? 1024 : -1;int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);// ensure null terminating on VSif (res >= 0 && res < len){buf[res] = 0;return res;}else{buf[len - 1] = 0; // truncate happenedreturn res >= len ? res : (len * 2);}
#elsereturn vsnprintf(buf, len, fmt, args);
#endif
}String format( const char* fmt, ... )
{AutoBuffer<char, 1024> buf;for ( ; ; ){va_list va;va_start(va, fmt);int bsize = static_cast<int>(buf.size());int len = cv_vsnprintf(buf.data(), bsize, fmt, va);va_end(va);CV_Assert(len >= 0 && "Check format string for errors");if (len >= bsize){buf.resize(len + 1);continue;}buf[bsize - 1] = 0;return String(buf.data(), len);}
}//! @addtogroup core_utils
//! @{/** @brief  Automatically Allocated Buffer ClassThe class is used for temporary buffers in functions and methods.If a temporary buffer is usually small (a few K's of memory),but its size depends on the parameters, it makes sense to create a smallfixed-size array on stack and use it if it's large enough. If the required buffer sizeis larger than the fixed size, another buffer of sufficient size is allocated dynamicallyand released after the processing. Therefore, in typical cases, when the buffer size is small,there is no overhead associated with malloc()/free().At the same time, there is no limit on the size of processed data.This is what AutoBuffer does. The template takes 2 parameters - type of the buffer elements andthe number of stack-allocated elements. Here is how the class is used:\codevoid my_func(const cv::Mat& m){cv::AutoBuffer<float> buf(1000); // create automatic buffer containing 1000 floatsbuf.allocate(m.rows); // if m.rows <= 1000, the pre-allocated buffer is used,// otherwise the buffer of "m.rows" floats will be allocated// dynamically and deallocated in cv::AutoBuffer destructor...}\endcode
*///! @} core_utils

C++ format string (格式化字符串)相关推荐

  1. Python中该使用%还是format来格式化字符串?

    %还是format 1.皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是fo ...

  2. Python中应该使用%还是format来格式化字符串?

    %还是format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是form ...

  3. python里面format什么时候用_Python:Python中应该使用%还是format来格式化字符串?...

    %还是format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? Python2.6引入了format这个格式化字符串的方法下面两者进行下比较 # 定义一 ...

  4. python 为什么用%格式化_Python中应该使用%还是format来格式化字符串?

    原标题:Python中应该使用%还是format来格式化字符串? Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字 ...

  5. 变量与字符串的连接 - format、格式化字符串

    变量与字符串的连接 先当以如下三个变量: name='wwb' age='17' job='study' 方法一:利用+号进行连接 >>>info1='''Welcome to '' ...

  6. Python中用format函数格式化字符串的用法

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  7. CString、std::string格式化字符串

    2019独角兽企业重金招聘Python工程师标准>>> =============================CString=========================== ...

  8. Python中用format函数格式化字符串的用法(2.7版本讲解哦!)

    语法 它通过{}和:来代替%. "映射"示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{ ...

  9. Oracle 异常 ORA-01861: literal does not match format string(字符串格式不匹配)

    如果直接按照字符串方式,或者,直接使用'2007-12-28 10:07:24',没有指定日期格式,就会报错:ORA-01861: 文字与格式字符串不匹配 必须指定日期格式 报错 SELECT D26 ...

最新文章

  1. centos下为firefox安装flash插件的几种方法
  2. 【剑指offer-Java版】08旋转数组的最小数字
  3. 试说明机器指令和微指令之间的关系_男女关系之间,是他在说谎吗?观察他的肢体语言说明一切...
  4. mongodb用户管理简单记录
  5. Asp程序做页面的301跳转,使得cnasda.com跳转到www.cnasda.com
  6. WordPress环境配置与安装
  7. 数字图像处理应用领域
  8. 金牛判势 精品起涨预警 主升浪起爆点指标 通达信指标公式
  9. 小程序canvas画入圆形图片
  10. [附源码]java毕业设计高校学生疫情防控信息管理系统
  11. 基于ESP32的蓝牙鼠标键盘(二)BleMouse.h函数解析
  12. 程序员如何摆脱天天CRUD这种打杂状态?
  13. 【JVM学习笔记】-字节码-第一章(多图,多表,实操)
  14. android 窗口圆角
  15. Tagged Pointer分析
  16. python中基例是什么意思_python中 *= 是什么意思
  17. 华子20221012笔试第三题
  18. 华尔街日报:郭台铭的“紫禁城”
  19. 用位运算完成大小比较
  20. leetcode刷题:顺丰科技智慧物流校园技术挑战赛

热门文章

  1. 计算机发生死机故障时 重新启动机器,超星尔雅高三语文 诗歌鉴赏复习章节测试答案...
  2. 腾讯云轻量应用服务器搭建wordpress之发送注册验证邮件
  3. 大数据day08笔记
  4. python使用ddt_python中,ddt模块的使用
  5. Verilog代码BCD计数器
  6. 《Adobe Flash CS6中文版经典教程》——1.10 修改内容和舞台
  7. 剪切板 html 查看器,剪贴板历史查看器
  8. android view 半透明图片,Android ImageView 图片设置为透明
  9. Itext旋转PDF内容的方法
  10. AT89C51驱动共阴极数码管的应用代码