<Boost> 字符串处理和格式化输出

本文介绍Boost库中的字符串处理工具,包括字符串的大小写转换、比较、修剪、查找、格式化输出及分割等功能,并通过示例代码展示具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

boost有丰富的字符串处理和格式化输出库。

boost::string_algo 

      用于字符串大小写转换,比较,修剪和查找

boost::format

      用于字符串格式化输出,如printf一般.

boost::tokenizer

      用于字符串分割


下面是测试代码:

#include <boost/algorithm/string.hpp>
#include <boost/format.hpp> 
#include <boost/tokenizer.hpp>

void TestStringFormat()
{
	//////////////////////////////////////////////////////////////////////////
	// 字符串处理和格式化输出
	//////////////////////////////////////////////////////////////////////////
	// string_algo: 字符串大小写转换,比较,修剪,特定字符串的查找
	string str1 = "  ..this iS My String";
	cout << "Original : " << str1 << endl;
	cout << "End With \"ing\": " << boost::ends_with(str1, "ing")  << endl;	// 判断是否以"ing"结尾
	cout << "After to upper : " << boost::to_upper_copy(str1) << endl;;		// 转换为大写, _copy:不改变原字符串
	cout << "Trim: " << boost::trim_left_copy(str1) << endl;;			// 删除左侧的空白字符
	boost::replace_first(str1, "this", "thez");			// 替换"this"为"thez"
	cout << "After replace: " << str1  << endl;

	boost::iterator_range<std::string::iterator> ir = boost::find_first(str1, "ing");
	cout << "Find \"ing\": " << string(ir.begin(), ir.end()) << endl;

	vector<char> v(str1.begin(),str1.end());
	vector<char> v2= boost::to_upper_copy(boost::erase_first_copy(v, "String")); // 删除"String"部分之后转换为大写字符,赋值v2
	for(int i=0; i<v2.size();++i)
	{
		cout << v2[i];
	}
	cout << endl;

	//////////////////////////////////////////////////////////////////////////
	// format: 格式化输出
	cout <<  
		boost::format("date: %05d, %.2f, %s") 
		% 2304
		% 343.2
		% "my"
		<<endl; 

	//////////////////////////////////////////////////////////////////////////
	// tokenizer 分割
	string strLine = "this is the\ntest\tline for,tokenizer.hello";
	boost::tokenizer<> tok(strLine);	// 缺省的情况下,以空白字符,标点作为分隔字符

	for (boost::tokenizer<>::iterator it = tok.begin(); it != tok.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;
	// char_separator
	boost::char_separator<char> cs;	
	boost::tokenizer<boost::char_separator<char> > tokcs(strLine, cs); // 默认保留标点但将它看作分隔符

	for (boost::tokenizer<boost::char_separator<char> >::iterator it = tokcs.begin(); it != tokcs.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;

	// char_separator自定义
	string strLine1 = "this|is|the-test*Line for,tokenizer";
	boost::char_separator<char> custcs("|-*");	// 自己指定
	boost::tokenizer<boost::char_separator<char> > tokcs1(strLine1, custcs); 

	for (boost::tokenizer<boost::char_separator<char> >::iterator it = tokcs1.begin(); it != tokcs1.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;

	// offset_separator
	string strLine2 = "abc bcd cde def efg fgh";
	boost::replace_all(strLine2, " ", "");
	int offsets[] = {3};
	boost::offset_separator os(offsets, offsets+1);	// 每隔3个字符
	boost::tokenizer<boost::offset_separator> tokos(strLine2, os);
	for (boost::tokenizer<boost::offset_separator>::iterator it = tokos.begin(); it != tokos.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;
} 


运行效果图:



<think>我们被要求回答关于C++字符串格式化输出的问题。根据引用内容,我们可以总结三种主要方法: 1. 使用C风格的snprintf(需要包含<stdio.h>) 2. 使用Boost库的format(需要安装Boost) 3. 使用C++标准库的stringstream(包含<sstream>,无需额外库) 另外,引用[3]还提到使用ostringstreamC++的格式控制符(如setw)进行格式化。 用户的问题是如何在C++中实现字符串格式化输出,我们将分别介绍这几种方法,并给出示例代码。 注意:由于用户要求参考站内引用,我们将结合引用[1][2][3]的内容,但重点放在格式化输出字符串(即生成一个格式化后的字符串)上。 步骤: 1. 介绍每种方法的基本用法 2. 给出示例代码 3. 比较不同方法的优缺点 由于引用[1]提到printfscanf,但这里我们只关注输出字符串,所以主要使用snprintf(对应sprintf的安全版本)C++的流方法。 我们将按照以下顺序: 方法1:使用snprintf(C风格,安全版本) 方法2:使用stringstream(C++标准,类型安全,但格式控制稍复杂) 方法3:使用boost::format(需要Boost,提供类似printf的格式字符串) 另外,我们还可以介绍C++20引入的std::format(如果用户环境支持C++20,则推荐使用,但引用中没有提到,所以作为补充)。 开始回答:</think>### C++ 字符串格式化输出方法详解 在 C++ 中实现字符串格式化输出主要有四种方法,根据需求选择合适的方式: #### 1. **C 风格 `snprintf()`(安全格式化)** 最接近 C 语言的解决方案,适合熟悉 `printf` 语法的开发者: ```cpp #include <cstdio> #include <string> int main() { std::string name = "Alice"; int age = 30; double score = 95.5; // 预分配缓冲区(避免溢出) char buffer[256]; // 格式化输出到缓冲区 int len = snprintf( buffer, sizeof(buffer), "Name: %s, Age: %d, Score: %.1f", name.c_str(), age, score ); // 转换为 std::string std::string result(buffer, len > 0 ? len : 0); std::cout << result; // 输出: Name: Alice, Age: 30, Score: 95.5 } ``` **优点**:执行效率高,格式控制灵活 **缺点**:需手动管理缓冲区,类型不安全[^2] --- #### 2. **`std::stringstream`(标准库解决方案)** 类型安全的 C++ 原生方案,推荐用于简单格式化: ```cpp #include <sstream> #include <iomanip> // 用于格式控制 int main() { std::string product = "Laptop"; double price = 2499.99; int quantity = 3; std::stringstream ss; ss << "Product: " << product << "\nPrice: $" << std::fixed << std::setprecision(2) << price << "\nTotal: $" << price * quantity; std::string result = ss.str(); std::cout << result; /* 输出: Product: Laptop Price: $2499.99 Total: $7499.97 */ } ``` **优点**:类型安全,无需管理缓冲区,支持自定义格式(如 `std::setw()`、`std::setprecision()`)[^3] **缺点**:复杂格式时代码冗长 --- #### 3. **`boost::format`(第三方库)** 需安装 Boost 库,提供类似 Python 的格式化语法: ```cpp #include <boost/format.hpp> int main() { int x = 10, y = 20; boost::format fmt("计算结果: %1% + %2% = %3%"); fmt % x % y % (x + y); // 按位置注入参数 std::string result = fmt.str(); std::cout << result; // 输出: 计算结果: 10 + 20 = 30 } ``` **优点**:语法简洁直观,支持位置参数 **缺点**:需额外安装 Boost 库[^2] --- #### 4. **C++20 `std::format`(现代解决方案)** C++20 原生支持的现代化方案(需编译器支持 C++20): ```cpp #include <format> // C++20 头文件 int main() { std::string lang = "C++"; int version = 20; // 类似 Python 的格式化语法 std::string result = std::format( "语言: {} | 版本: {}\n浮点数: {:.2f}", lang, version, 3.14159 ); std::cout << result; /* 输出: 语言: C++ | 版本: 20 浮点数: 3.14 */ } ``` **优点**:简洁高效,类型安全,无需外部依赖 **缺点**:需 C++20 支持(GCC>=13, Clang>=15, MSVC>=19.30) --- ### 方法对比与选择建议 | 方法 | 易用性 | 安全性 | 性能 | 依赖项 | |--------------------|--------|--------|------|--------------| | `snprintf` | ★★☆ | ★★☆ | ★★★ | 无(需缓冲区)| | `std::stringstream`| ★★★ | ★★★ | ★★☆ | C++ 标准库 | | `boost::format` | ★★★ | ★★★ | ★★☆ | Boost 库 | | `std::format` | ★★★ | ★★★ | ★★★ | C++20 标准 | **推荐场景**: - 兼容旧项目:`snprintf`(注意缓冲区安全) - 简单格式化:`std::stringstream` - 新项目开发:优先使用 `std::format`(C++20) - 需要高级格式化:`boost::format` > 所有数值格式化均可通过 `<iomanip>` 中的 `std::hex`, `std::scientific` 等控制符实现精细控制[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值