反向迭代器reverse_Iterator的表示范围,以及反向迭代器和正向Iterator的转化关系见下图:
反向Iterator执行自增操作是向前移动,自减是向后移动
下面是一个使用Reverse_Iterator将一个句子进行反向输出的例子
std::string line = "Nothing is impossible";
std::string::reverse_iterator findIt;
std::string::reverse_iterator tempIt = line.rbegin();
findIt = find(line.rbegin(),line.rend(),' ');
//The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.
//In practice,if you want to use it with an object or a struct,you must overload the operator ==
while(findIt != line.rend())
{
cout<.base(),tempIt.base());//[)
//function base which convert reverse_iterator to iterator
cout<<" ";
tempIt = findIt + 1;
findIt = find(findIt+1,line.rend(),' ');//[)
}
//We need to do special process to the first word
cout<
cout<
阅读(2023) | 评论(0) | 转发(0) |