basic_string::rend
返回解决成功最后一个元素的位置在一个反转的字符串的迭代器。
const_reverse_iterator rend( ) const;
reverse_iterator rend( );
返回值
解决成功最后一个元素的位置在一个反转的字符串进行了反向随机访问迭代器。
备注
正 结束 用于字符串,rend 用于一个反转的字符串。
如果 rend 的返回值赋给 const_reverse_iterator,不能修改字符串对象。如果 rend 的返回值赋给 reverse_iterator,可以修改字符串对象。
rend 可用于测试一反向迭代器是否已到达其字符串的末尾。
不应取消引用 rend 返回的值。
示例
// basic_string_rend.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ("Able was I ere I saw Elba"), str2;
basic_string <char>::reverse_iterator str_rIter, str1_rIter, str2_rIter;
basic_string <char>::const_reverse_iterator str1_rcIter;
str1_rIter = str1.rend ( );
str1_rIter--;
cout << "The last character-letter of the reversed string str1 is: "
<< *str1_rIter << endl;
cout << "The full reversed string str1 is:\n ";
for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
cout << *str_rIter;
cout << endl;
// The dereferenced iterator can be used to modify a character
*str1_rIter = 'o';
cout << "The last character-letter of the modified str1 is now: "
<< *str1_rIter << endl;
cout << "The full modified reversed string str1 is now:\n ";
for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
cout << *str_rIter;
cout << endl;
// The following line would be an error because iterator is const
// *str1_rcIter = 'T';
// For an empty string, end is equivalent to begin
if ( str2.rbegin( ) == str2.rend ( ) )
cout << "The string str2 is empty." << endl;
else
cout << "The stringstr2 is not empty." << endl;
}
要求
标头: <string>
命名空间: std