12.红黑树set

 1 #include <iostream>
 2 //红黑树(自动保证平衡,自动生成平衡查找树)
 3 #include <set>
 4 #include <cstring>
 5 #include <cstdlib>
 6 using namespace std;
 7 
 8 //红黑树增删查改
 9 void main1()
10 {
11     set<int> myset{ 1,2,10,3,5 };
12     myset.insert(14);
13     myset.insert(15);
14     //重复的数据会被忽略
15     myset.insert(15);
16 
17     //中序遍历
18     for (auto i : myset)
19     {
20         cout << i << endl;
21     }
22 
23     //正向迭代
24     for (auto ib = myset.begin(), ie = myset.end(); ib != ie; ib++)
25     {
26         cout << *ib << endl;
27     }
28 
29     //反向迭代
30     for (auto rb = myset.rbegin(), re = myset.rend(); rb != re; rb++)
31     {
32         cout << *rb << endl;
33     }
34 
35     auto it = myset.find(14);
36     if (it != myset.end())
37     {
38         cout << "find" << *it << endl;
39         //不能修改
40         //*it = 9;
41         //删除
42         myset.erase(it);
43         //或者
44         //myset.erase(10);
45     }
46     else
47     {
48         cout << "not find" << *it << endl;
49     }
50 
51     //元素个数
52     myset.size();
53     //清空
54     myset.empty();
55     cin.get();
56 }
57 
58 struct strless
59 {
60     bool operator()(const char *str1, const char *str2)
61     {
62         return (strcmp(str1, str2) < 0);
63     }
64 };
65 
66 void main()
67 {
68     const char *p[] = { "calc","notepad","run","go" ,"123"};
69     //设置比较函数 前边是类型,后边是比较的函数
70     set<const char *, strless> myset(p, p + 5, strless());
71     myset.insert("234");
72 
73     for (auto i : myset)
74     {
75         cout << i << endl;
76     }
77     cin.get();
78 }

 

posted @ 2018-03-22 22:39  喵小喵~  阅读(150)  评论(0编辑  收藏  举报