Learning boost 3
string algorithm 1
简介
string algorithm是boost中提供字符串算法的类库。在这里,字符串不一定是std::basic_string,也可以是其它的stl容器或是c++中的零结尾字符串char*。
例:
std::string str("Hello");
std::vector<char> vstr(str.begin(), str.end());
char *cstr=new char(str.size());
std::copy(str.begin(), str.end(), cstr);
boost::to_upper(str); //string
boost::to_upper(vstr); //vector<char>
boost::to_upper(cstr); //char*
另外,在string algorithm中的很多算法都分为mutable 和copy,mutable算法直接修改输入的字符串,而copy算法返回一个新的字符串或通过一个迭代器输出。
在string algorithm中碰到一个IteratorRange的概念,这是boost::range中的一个类,我也不会使用,不过可以使用make_iterator_range(iterator, iterator)来构造。iterator_range通过两个开始和结束迭代器表示一段范围,一般通过查找算法的返回来得到。
例:
std::string str1("Hello");
std::string str2("Hello");
boost::to_upper(str1);//mutable algorithm,现在str1==”HELLO”
std::string str3=boost::to_upper_copy(str2);//copy algorithm, str3==”HELLO”
/*
char * p =boost::to_upper_copy("Hello");
这是错的。因为to_upper_copy不会去申请一段内存保存"Hello"的拷贝。
char str[]={"Hello"};
to_upper(str);这样这就可以了。
*/
std::string str4;
boost::to_upper_copy(std::back_inserter(str4),
std::make_pair(str2.begin(), str2.end())
);//使用迭代器的copy algorithm, str4==”HELLO”
//这里注意,这里和stl不同的是输出迭代器在输入迭代器的前面
boost::to_upper_copy(std::back_inserter(str4), "Hello");str4==”HELLOHELLO”
//这也是可以的,前面说过了IteratorRange相当于一个容器,
//stl标准容器,std::basic_string 和char*都可以作为string_algorithm中的容器
Case Conversion Algorithm 大小写转换算法
void to_lower(MutableCollection&, std::local&=std::local());
void to_upper(MutableCollection&, std::local&=std::local());
Sequence to_lower_copy(const Sequence&, std::local&=std::local());
Sequence to_upper_copy(const Sequence&, std::local&=std::local());
OutputIterator to_lower_copy(OutputIterator, const Collection&, std::local&=std::local());
OutputIterator to_lower_copy(OutputIterator, const Collection&, std::local&=std::local());
在string algorithm中,Collection指:
l stl标准类型
l 内建数组(如:int[])
l 零结尾字符串(char*或wchar *)
l pair<iterator, iterator>
MutableCollection当然是指非const 的collection。
Sequence好像只能是标准容器。
Trim Algorithm Trim算法
void trim (Sequence&, std::local&=std::local());
void trim_left (Sequence&, std::local&=std::local());
void trim_right (Sequence&, std::local&=std::local());
Sequence trim_copy (const Sequence&, std::local&=std::local());
Sequence trim_left_copy (const Sequence&, std::local&=std::local());
Sequence trim_right_copy (const Sequence&, std::local&=std::local());
上述算法删除字符串中左右(或是其中之一)的空格。
void trim_if (Sequence&, Predicate);
void trim_left_if (Sequence&, Predicate);
void trim_right_if(Sequence&, Predicate);
Sequence trim_copy_if (const Sequence&, Predicate);
Sequence trim_left_copy_if (const Sequence&, Predicate);
Sequence trim_right_copy_if(const Sequence&, Predicate);
OutputIterator trim_copy_if (OutputIterator, const Collection&, Predicate);
OutputIterator trim_left_copy_if (OutputIterator, const Collection&, Predicate);
OutputIterator trim_right_copy_if(OutputIterator, const Collection&, Predicate);
上述算法删除字符串左右(或是其中之一)满足Predicate的字符。
Predicate是一个一元判断式。
例:
string str(“---Hello---”);
boost::trim_if(str, bind2nd(equal_to<char>(), ’-‘));//str==”Hello”
Predicate 判断式
string algorithm中提供了一些与字符串相关的判断式。
bool starts_with (Input, Test);
bool starts_with (Input, Test, Comp)
bool istarts_with(Input, Test, Local)
判断Input是否以Test开头。Comp是比较字符的二元判断式。Local是std::local。
bool ends_with (Input, Test);
bool ends_with (Input, Test, Comp)
bool iends_with(Input, Test, Local)
判断Input是否以Test结尾。Comp是比较字符的二元判断式。Local是std::local。
bool contains (Input, Test);
bool contains (Input, Test, Comp)
bool icontains(Input, Test, Local)
判断Input是否包含Test。Comp是比较字符的二元判断式。Local是std::local。
bool equals (Input, Test);
bool equals (Input, Test, Comp)
bool iequals(Input, Test, Local)
判断Input是否和Test相同。Comp是比较字符的二元判断式。Local是std::local。
bool all(Input, Pred);
判断Input的每一个字符是否满足Pred判断式。
Classfication 类别判断式
classfication也是判断式,它们是对std::isspace等函数的泛型化封装。
unspecified is_classified(std::ctype_base::mask, const std::locale & = std::locale());
unspecified is_space (const std::locale & = std::locale());
unspecified is_alnum (const std::locale & = std::locale());
unspecified is_alpha (const std::locale & = std::locale());
unspecified is_cntrl (const std::locale & = std::locale());
unspecified is_digit (const std::locale & = std::locale());
unspecified is_graph (const std::locale & = std::locale());
unspecified is_lower (const std::locale & = std::locale());
unspecified is_print (const std::locale & = std::locale());
unspecified is_punct (const std::locale & = std::locale());
unspecified is_upper (const std::locale & = std::locale());
unspecified is_xdigit (const std::locale & = std::locale());
判断字符是否是某个类型,返回判断式。
例:
trim_copy_if(string(“123Hello
unspecified is_any_of(Set);
判断字符是否是Set中的一个,返回判断式。
unspecified is_from_range(From, To);
判断字符是否是否处在From和To之间(From<=Ch<=To)。
unspecified operator&&(Pred1, Pred2);
unspecified operator||(Pred1, Pred2);
unspecified operator!(Pred);
classfication判断式的与或非运算。注意:只能对上述的classfication判断式使用。
例:
trim_copy_if(string(“123Hello
iterator_range Class 迭代器范围类
iterator_range是对一组迭代器的封装。一般用于find算法的返回值。
iterator_range有类似于stl容器的begin(), end(), empty(), size(), swap(), bool operator()成员函数。
一般使用make_iterator_range()来构造iterator_rang,make_iterator_range使用两个迭代器或者是pair<iterator, iterator>作为参数。
还可以使用流来输出iterator_range。
例:
string str(“Hello”);
cout<<make_iterator_range(str.begin()+1, str.end()-1);//output “ell”
对于iterator_range的ostream输出类似于 copy(it.begin(),it.end(),ostream_iterator<char>(“”));
无空格的连续输出。
Erase Algorithm 删除算法
OutputIterator erase_range_copy (OutputIterator, Input, SearchRange);
Sequence erase_range_copy (Input, SearchRange);
void erase_range (Input, SearchRange);
删除输入字符串中SearchRange那部分。SearchRange是一个iterator_range对象。
例:
std::string str1("Hello");
std::string str2;
boost::erase_range(str1, boost::make_iterator_range(str1.begin()+1, str1.end()-1));
cout<<str1<<endl;//输出”Ho”
erase_range_copy(back_inserter(str2),
str1,
boost::make_iterator_range(str1.begin()+1, str1.end()-1));
cout<<str2<<endl;//输出”Ho”
OutputIterator erase_first_copy(OutputIterator, Input, Search);
Sequence erase_first_copy(Input, Search);
void erase_first (Input, Search);
OutputIterator ierase_first_copy(OutputIterator, Input, Search, Local);
Sequence ierase_first_copy(Input, Search, Local);
void ierase_first (Input, Search, Local);
删除从Input中查找到的第一个Search。
例:
std::string str1("Hello");
std::string str2;
boost::erase_first(str1, "ll");
str2=boost::erase_first_copy(string("Hello"), "l");
cout<<str1<<endl;//输出”Heo”
cout<<str2<<endl;//输出”Helo”