C++字符串格式化的几种方式

  1. 使用snprintf格式化字符串
  2. 使用boost::format格式化字符串
  3. 使用stringstream格式化字符串

具体示例

  1. 使用snprintf格式化字符串
#include <stdio.h>
using std::string;
// 准备数据
string haha("haha");
int num = 3;
// 准备格式
string fmt("test string: %s. test number: %d");
char targetString[1024];
// 格式化,并获取最终需要的字符串
int realLen = snprintf( targetString, 
						sizeof(targetString), 
						fmt.c_str(), 
						haha.c_str(), 
						num );

参考链接http://www.cplusplus.com/reference/cstdio/snprintf/

  1. 使用boost::format格式化字符串
#include "boost/format.hpp"
// 准备数据
string haha("haha");
int num = 3;
// 准备格式
boost::format fmt("test string: %s. test number: %d");
// 格式化
fmt % haha % num;
// 获取最终需要的字符串
string targetString = fmt.str();

参考链接https://www.boost.org/doc/libs/1_70_0/libs/format/example/sample_formats.cpp

  1. 使用stringstream格式化字符串
#include <sstream>
using std::stringstream;
// 准备数据
string haha("haha");
int num = 3;
// 准备根据格式造字符串流
stringstream fmt;                       /* 或者使用 ostringstream */
// 造字符串流
fmt << "test string: " << haha << ". test number: " << num;
// 获取最终需要的字符串
string targetString = fmt.str();

参考链接http://www.cplusplus.com/reference/ostream/ostream/operator<</

  • 13
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C++ 中,我们可以使用多种方式字符串打印输出,下面介绍几种常见的方式: 1. 使用 `printf` 函数和格式化字符串C++ 中,我们可以使用 `printf` 函数和格式化字符串将矩阵以字符串形式输出。具体代码如下: ```c++ #include <cstdio> #include <string> int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::string str; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { char buf[10]; sprintf(buf, "%d ", matrix[i][j]); str += buf; } str += '\n'; } printf("%s", str.c_str()); return 0; } ``` 在上述代码中,我们首先定义了一个3*3的矩阵和一个字符串变量 `str`,然后使用 `sprintf` 函数将矩阵元素转换成字符串,并将其拼接到 `str` 中。最后,我们使用 `printf` 函数输出 `str`。 2. 使用 `stringstream` 类 在 C++ 中,我们还可以使用 `stringstream` 类将矩阵以字符串形式输出。具体代码如下: ```c++ #include <cstdio> #include <sstream> #include <string> int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::stringstream ss; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ss << matrix[i][j] << ' '; } ss << '\n'; } std::string str = ss.str(); printf("%s", str.c_str()); return 0; } ``` 在上述代码中,我们首先定义了一个3*3的矩阵和一个 `stringstream` 对象 `ss`,然后使用重载的 `<<` 运算符将矩阵元素拼接到 `ss` 中。最后,我们使用 `ss.str()` 函数将 `ss` 转换成字符串,并将其赋值给字符串变量 `str`。最终,我们使用 `printf` 函数将 `str` 输出到控制台中。 3. 使用 `to_string` 函数 在 C++11 中,我们还可以使用 `to_string` 函数将整数转换成字符串,并将矩阵以字符串形式输出。具体代码如下: ```c++ #include <cstdio> #include <string> int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::string str; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { str += std::to_string(matrix[i][j]) + ' '; } str += '\n'; } printf("%s", str.c_str()); return 0; } ``` 在上述代码中,我们首先定义了一个3*3的矩阵和一个字符串变量 `str`,然后使用 `to_string` 函数将矩阵元素转换成字符串,并将其拼接到 `str` 中。最后,我们使用 `printf` 函数将 `str` 输出到控制台中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值