boost::to_upper_copy详解

本文介绍boost库中to_upper_copy函数的两种用法,包括直接转换字符串为大写及通过迭代器范围进行转换。提供了多个示例展示如何使用这些函数。

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

boost::to_upper_copy有两种构造函数,特别是第二种, 注意boost::range的使用
函数形式:
  1. template<typename OutputIteratorT, typename RangeT>  
  2. OutputIteratorT  
  3. to_upper_copy(OutputIteratorT Output, const RangeT & Input,  
  4. const std::locale & Loc = std::locale());  
  5. template<typename SequenceT>  
  6. SequenceT to_upper_copy(const SequenceT & Input,  
  7. const std::locale & Loc = std::locale());  
例子:
  1. // to_upper_copy两种重载函数  
  2. void test_string_upper()  
  3. {  
  4.     std::string str1 = "Hello world!";  
  5.   
  6.     // 方法1  
  7.     std::string str2 = boost::to_upper_copy(str1);  
  8.     assert(str2 == "HELLO WORLD!");  
  9.   
  10.     // 方法2, 第二个参数是range, 可以是如下几种类型  
  11.     // 1. make_pire(iterator, iterator)  
  12.     // 2. std::string  
  13.     // 3. 数组  
  14.     // 4. 标准库容器   
  15.     std::string str3 = "str3 = ";  
  16.     boost::to_upper_copy(std::back_inserter(str3), std::make_pair(str1.begin(), str1.end()));  
  17.     assert(str3 == "str3 = HELLO WORLD!");  
  18.   
  19.     std::string str4 = "str4 = ";  
  20.     boost::to_upper_copy(std::back_inserter(str4), "HELLO WORLD!");  
  21.     assert(str4 == "str4 = HELLO WORLD!");  
  22.   
  23.     std::string str5 = "str5 = ";  
  24.     boost::to_upper_copy(std::back_inserter(str5), boost::make_iterator_range(str1));  
  25.     assert(str5 == "str5 = HELLO WORLD!");  
  26.   
  27.     std::string str6 = "str6 = ";  
  28.     char* p = "HELLO WORLD!";  
  29.     boost::to_upper_copy(std::back_inserter(str6), p);  
  30.     assert(str6 == "str6 = HELLO WORLD!");  
  31. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值