歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 【Boost】boost::to_upper_copy詳解

【Boost】boost::to_upper_copy詳解

日期:2017/3/1 10:05:44   编辑:Linux編程

boost::to_upper_copy有兩種構造函數,特別是第二種,注意boost::range的使用。

函數形式:

template<typename OutputIteratorT, typename RangeT>
OutputIteratorT
to_upper_copy(OutputIteratorT Output, const RangeT & Input,
const std::locale & Loc = std::locale());
template<typename SequenceT>
SequenceT to_upper_copy(const SequenceT & Input,
const std::locale & Loc = std::locale());

例子:

// to_upper_copy兩種重載函數
void test_string_upper()
{
std::string str1 = "Hello world!";

// 方法1
std::string str2 = boost::to_upper_copy(str1);
assert(str2 == "HELLO WORLD!");

// 方法2, 第二個參數是range, 可以是如下幾種類型
// 1. make_pire(iterator, iterator)
// 2. std::string
// 3. 數組
std::string str3 = "str3 = ";
boost::to_upper_copy(std::back_inserter(str3), std::make_pair(str1.begin(), str1.end()));
assert(str3 == "str3 = HELLO WORLD!");

std::string str4 = "str4 = ";
boost::to_upper_copy(std::back_inserter(str4), "HELLO WORLD!");
assert(str4 == "str4 = HELLO WORLD!");

std::string str5 = "str5 = ";
boost::to_upper_copy(std::back_inserter(str5), boost::make_iterator_range(str1));
assert(str5 == "str5 = HELLO WORLD!");

std::string str6 = "str6 = ";
char* p = "HELLO WORLD!";
boost::to_upper_copy(std::back_inserter(str6), p);
assert(str6 == "str6 = HELLO WORLD!");
}

Copyright © Linux教程網 All Rights Reserved