头文件
boost/algorithm/string/case_conv.hpp
作用
主要有如下API
to_lower_copy:将原来字符串,转换为小写字符串,并返回新的字符串,原来字符串不改变。
to_upper_copy:将原来字符串,转换为大写字符串,并返回新的字符串,原来字符串不改变。
to_lower:将原来字符串,转换为小写字符串,原来字符串改变。
to_upper:将原来字符串,转换为大写字符串,原来字符串改变。
举例
#include <boost/algorithm/string/case_conv.hpp>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace boost;
void conv_test()
{
string str1("AbCdEfG 123 xxxYYYzZzZ");
string str2("AbCdEfG 123 xxxYYYzZzZ");
string str3("");
const char pch[]="AbCdEfG 123 xxxYYYzZzZ";
unsigned int pchlen=sizeof(pch);
char* pch1=new char[pchlen];
std::copy(pch, pch+pchlen, pch1);
char* pch2=new char[pchlen];
std::copy(pch, pch+pchlen, pch2);
// *** iterator tests *** //
string strout;
to_lower_copy( back_inserter(strout), str1 );
BOOST_CHECK( strout=="abcdefg 123 xxxyyyzzzz" );
strout.clear();
to_upper_copy( back_inserter(strout), str1 );
BOOST_CHECK( strout=="ABCDEFG 123 XXXYYYZZZZ" );
strout.clear();
to_lower_copy( back_inserter(strout), "AbCdEfG 123 xxxYYYzZzZ" );
BOOST_CHECK( strout=="abcdefg 123 xxxyyyzzzz" );
strout.clear();
to_upper_copy( back_inserter(strout), "AbCdEfG 123 xxxYYYzZzZ" );
BOOST_CHECK( strout=="ABCDEFG 123 XXXYYYZZZZ" );
strout.clear();
to_lower_copy( back_inserter(strout), pch1 );
BOOST_CHECK( strout=="abcdefg 123 xxxyyyzzzz" );
strout.clear();
to_upper_copy( back_inserter(strout), pch1 );
BOOST_CHECK( strout=="ABCDEFG 123 XXXYYYZZZZ" );
// *** value passing tests *** //
BOOST_CHECK( to_lower_copy( str1 )=="abcdefg 123 xxxyyyzzzz" );
BOOST_CHECK( to_upper_copy( str1 )=="ABCDEFG 123 XXXYYYZZZZ" );
BOOST_CHECK( to_lower_copy( str3 )=="" );
BOOST_CHECK( to_upper_copy( str3 )=="" );
// *** inplace tests *** //
to_lower( str1 );
BOOST_CHECK( str1=="abcdefg 123 xxxyyyzzzz" );
to_upper( str2 );
BOOST_CHECK( str2=="ABCDEFG 123 XXXYYYZZZZ" );
// c-string modification
to_lower( pch1 );
BOOST_CHECK( string(pch1)=="abcdefg 123 xxxyyyzzzz" );
to_upper( pch2 );
BOOST_CHECK( string(pch2)=="ABCDEFG 123 XXXYYYZZZZ" );
to_lower( str3 );
BOOST_CHECK( str3=="" );
to_upper( str3 );
BOOST_CHECK( str3=="" );
delete[] pch1;
delete[] pch2;
}
// test main
int main(int argc, char* [])
{
conv_test();
return 0;
}
源代码
namespace boost {
namespace algorithm {
// to_lower -----------------------------------------------//
//! Convert to lower case
/*!
Each element of the input sequence is converted to lower
case. The result is a copy of the input converted to lower case.
It is returned as a sequence or copied to the output iterator.
\param Output An output iterator to which the result will be copied
\param Input An input range
\param Loc A locale used for conversion
\return
An output iterator pointing just after the last inserted character or
a copy of the input
\note The second variant of this function provides the strong exception-safety guarantee
*/
template<typename OutputIteratorT, typename RangeT>
inline OutputIteratorT
to_lower_copy(
OutputIteratorT Output,
const RangeT& Input,
const std::locale& Loc=std::locale())
{
return ::boost::algorithm::detail::transform_range_copy(
Output,
::boost::as_literal(Input),
::boost::algorithm::detail::to_lowerF<
typename range_value<RangeT>::type >(Loc));
}
//! Convert to lower case
/*!
\overload
*/
template<typename SequenceT>
inline SequenceT to_lower_copy(
const SequenceT& Input,
const std::locale& Loc=std::locale())
{
return ::boost::algorithm::detail::transform_range_copy<SequenceT>(
Input,
::boost::algorithm::detail::to_lowerF<
typename range_value<SequenceT>::type >(Loc));
}
//! Convert to lower case
/*!
Each element of the input sequence is converted to lower
case. The input sequence is modified in-place.
\param Input A range
\param Loc a locale used for conversion
*/
template<typename WritableRangeT>
inline void to_lower(
WritableRangeT& Input,
const std::locale& Loc=std::locale())
{
::boost::algorithm::detail::transform_range(
::boost::as_literal(Input),
::boost::algorithm::detail::to_lowerF<
typename range_value<WritableRangeT>::type >(Loc));
}
// to_upper -----------------------------------------------//
//! Convert to upper case
/*!
Each element of the input sequence is converted to upper
case. The result is a copy of the input converted to upper case.
It is returned as a sequence or copied to the output iterator
\param Output An output iterator to which the result will be copied
\param Input An input range
\param Loc A locale used for conversion
\return
An output iterator pointing just after the last inserted character or
a copy of the input
\note The second variant of this function provides the strong exception-safety guarantee
*/
template<typename OutputIteratorT, typename RangeT>
inline OutputIteratorT
to_upper_copy(
OutputIteratorT Output,
const RangeT& Input,
const std::locale& Loc=std::locale())
{
return ::boost::algorithm::detail::transform_range_copy(
Output,
::boost::as_literal(Input),
::boost::algorithm::detail::to_upperF<
typename range_value<RangeT>::type >(Loc));
}
//! Convert to upper case
/*!
\overload
*/
template<typename SequenceT>
inline SequenceT to_upper_copy(
const SequenceT& Input,
const std::locale& Loc=std::locale())
{
return ::boost::algorithm::detail::transform_range_copy<SequenceT>(
Input,
::boost::algorithm::detail::to_upperF<
typename range_value<SequenceT>::type >(Loc));
}
//! Convert to upper case
/*!
Each element of the input sequence is converted to upper
case. The input sequence is modified in-place.
\param Input An input range
\param Loc a locale used for conversion
*/
template<typename WritableRangeT>
inline void to_upper(
WritableRangeT& Input,
const std::locale& Loc=std::locale())
{
::boost::algorithm::detail::transform_range(
::boost::as_literal(Input),
::boost::algorithm::detail::to_upperF<
typename range_value<WritableRangeT>::type >(Loc));
}
} // namespace algorithm
// pull names to the boost namespace
using algorithm::to_lower;
using algorithm::to_lower_copy;
using algorithm::to_upper;
using algorithm::to_upper_copy;
} // namespace boost