1个回答
展开全部
#include <set>
#include <iostream>
int main( )
{
using namespace std;
set <int> s1;
set <int>::iterator s1_Iter;
set <int>::reverse_iterator s1_rIter;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
s1_rIter = s1.rbegin( );
cout << "The first element in the reversed set is "
<< *s1_rIter << "." << endl;
// begin can be used to start an iteration
// throught a set in a forward order
cout << "The set is:";
for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
cout << " " << *s1_Iter;
cout << endl;
// rbegin can be used to start an iteration
// throught a set in a reverse order
cout << "The reversed set is:";
for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
cout << " " << *s1_rIter;
cout << endl;
// A set element can be erased by dereferencing to its key
s1_rIter = s1.rbegin( );
s1.erase ( *s1_rIter );
s1_rIter = s1.rbegin( );
cout << "After the erasure, the first element "
<< "in the reversed set is "<< *s1_rIter << "." << endl;
}
这是MSDN上 reverse_iterator 逆向迭代器的例子。 rbegin,rend 返回逆向迭代器
输入的结果是:
Output
The first element in the reversed set is 30.
The set is: 10 20 30
The reversed set is: 30 20 10
After the erasure, the first element in the reversed set is 20.
可以看出reverse_iterator 通过rbegin得到的是集合的最后一个元素,于iterator通过begin得到第一个元素正好相反。 rend也同理
#include <iostream>
int main( )
{
using namespace std;
set <int> s1;
set <int>::iterator s1_Iter;
set <int>::reverse_iterator s1_rIter;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
s1_rIter = s1.rbegin( );
cout << "The first element in the reversed set is "
<< *s1_rIter << "." << endl;
// begin can be used to start an iteration
// throught a set in a forward order
cout << "The set is:";
for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
cout << " " << *s1_Iter;
cout << endl;
// rbegin can be used to start an iteration
// throught a set in a reverse order
cout << "The reversed set is:";
for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
cout << " " << *s1_rIter;
cout << endl;
// A set element can be erased by dereferencing to its key
s1_rIter = s1.rbegin( );
s1.erase ( *s1_rIter );
s1_rIter = s1.rbegin( );
cout << "After the erasure, the first element "
<< "in the reversed set is "<< *s1_rIter << "." << endl;
}
这是MSDN上 reverse_iterator 逆向迭代器的例子。 rbegin,rend 返回逆向迭代器
输入的结果是:
Output
The first element in the reversed set is 30.
The set is: 10 20 30
The reversed set is: 30 20 10
After the erasure, the first element in the reversed set is 20.
可以看出reverse_iterator 通过rbegin得到的是集合的最后一个元素,于iterator通过begin得到第一个元素正好相反。 rend也同理
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询