写题遇到的Set误区

C++ set rbegin() 函数用于返回引用集合容器最后一个元素的反向迭代器。

set 的反向迭代器以相反的方向移动并递增它,直到它到达 set 容器的开头(第一个元素)。

用法

1
2
3
4
reverse_iterator rbegin();                            //until C++ 11
const_reverse_iterator rbegin() const; //until C++ 11
reverse_iterator rbegin() noexcept; //since C++ 11
const_reverse_iterator rbegin() const noexcept; //since C++ 11

参数

返回值

它返回一个反向迭代器(反向迭代器),它指向集合的最后一个元素。

复杂度

恒定。

迭代器有效性

没有变化。

数据竞争

非常量和常量版本都不会访问集合,不会修改集合容器。同时访问集合的元素是安全的。

异常安全

此函数从不抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <set>

using namespace std;

int main ()
{
set<int> myset= {10,50,30,40,20};

// show content:
cout<<"Elements are:"<<endl;
set<int>::reverse_iterator rit;
for (rit=myset.rbegin(); rit!=myset.rend(); ++rit)
cout << *rit<< '\n';

return 0;
}

输出:

1
2
3
4
5
6
Elements are:
50
40
30
20
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <set>
#include <string>
#include <iterator>

using namespace std;

int main() {

// Creating & Initializing a set of String
set<string> setEx = {"aaa", "ccc", "ddd", "bbb"};

// Create a set iterator and point to the end of set
set<string, int>::reverse_iterator it = setEx.rbegin();

// Iterate over the set using Iterator till beginning.
while (it != setEx.rend()) {
// Accessing KEY from element pointed by it.
string word = *it;

cout << word << endl;

// Increment the Iterator to point to next entry
it++;
}
return 0;
}

输出:

1
2
3
4
ddd
ccc
bbb
aaa

因为 set 以键的排序顺序存储元素,因此迭代集合将导致上述顺序,即键的排序顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;

return 0;
}

输出:

1
2
3
4
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
set<int> marks = {400, 350, 465, 290, 410};

cout << "Marks" << '\n';
cout<<"______________________\n";

set<int>::reverse_iterator rit;
for (rit=marks.rbegin(); rit!=marks.rend(); ++rit)
cout << *rit<< '\n';

auto ite = marks.rbegin();

cout << "\nHighest Marks is:"<< *ite <<" \n";

return 0;
}

输出:

1
2
3
4
5
6
7
8
9
Marks
______________________
465
410
400
350
290

Highest Marks is:465

Set重载小于号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <set>
using namespace std;
struct song
{
int m_id;
int m_hot;
song(int id,int hot)
{

this->m_id = id;
this->m_hot = hot;
}
bool operator<(const struct song & right)const //重载<运算符
{
if(this->m_id == right.m_id) //根据id去重
return false;
else
{
if(this->m_hot != right.m_hot)
{
return this->m_hot > right.m_hot; //降序
}
else
{
return this->m_id > right.m_id;
}
}
}
};
void main()
{
std::set<song> mySet;
song s1(10,100);
song s2(20,200);
song s3(20,300);
song s4(30,200);
mySet.insert(s1); //插入s1
mySet.insert(s2); //插入s2
mySet.insert(s3); //s3和s2的id相同,不插入
mySet.insert(s4); //插入s4
for(auto it:mySet)
{
std::cout<<"id:"<<it.m_id<<",hot:"<<it.m_hot<<std::endl;
}
std::cout<<"end"<<std::endl;
};
1
2
3
4
结果如下:
id:30,hot : 200
id:20,hot : 200
id:10,hot : 100end