set红黑树集合节点为节点或者链表

  1 // set
  2 
  3 #include<iostream>
  4 #include<set>
  5 #include<cstring>
  6 
  7 using namespace std;
  8 
  9 void main()
 10 {
 11     set<int> myset = {1,2,10,4,6};
 12 
 13     myset.insert(15);
 14     myset.insert(19);
 15     myset.insert(19);
 16     myset.insert(19);// 重复的会被忽略
 17 
 18 // 正向 从小到大
 19     for(auto ib = myset.begin(),ie = myset.end();ib!=ie;ib++)
 20     {
 21         cout << *ib << endl;
 22     }
 23 // 反向从大到小    
 24     for(auto rb = myset.rbegin(),re = myset.rend();rb!=ie;rb++)
 25     {
 26         cout << *rb << endl;
 27     }
 28 
 29     for(auto i : myset)
 30     {
 31         cout << i << endl;
 32     }
 33     
 34 
 35     auto ifind = myset.find(15);
 36 
 37     if(ifind!=myset.end())
 38     {
 39         cout << "find = " << *ifind << endl;
 40     }
 41     
 42     myset[3];// 不行
 43 
 44     cout << myset.size() << endl;
 45 
 46     cin.get();
 47 }
 48 
 49 //------------------------------------------------------------------------
 50 
 51 struct strless
 52 {
 53     bool operator ()(const char *str1,const char *str2)
 54     {
 55         return (strcmp(str1,str2) < 0);// 对比字符串,返回值为布尔
 56     }
 57 };
 58 
 59 void main()
 60 {
 61     const char *p[] = {"calc","notepad","run","go","abc","z123"};
 62     
 63     set<const char*,strless> myset(p,p+6,strless());// 有初始化数据的时候传入strless()
 64     
 65     myset.insert("acalc");
 66     myset.insert("xacalc");
 67 
 68     for(auto i : myset)
 69     {
 70         cout << i << endl;
 71     }
 72 
 73 
 74     cin.get();
 75 }
 76 
 77 //------------------------------------------------------------------------
 78 
 79 
 80 void main()
 81 {
 82 
 83     set<const char*> myset;
 84     myset.insert("523");// 比较常量字符串地址
 85     myset.insert("423");
 86     myset.insert("823");
 87     myset.insert("223");
 88     myset.insert("123");
 89     myset.insert("323");
 90 
 91     for(auto i : myset)
 92     {
 93         cout << i << endl;
 94     }
 95 
 96     set<const char*,strless> myAset;// 没有初始化数据就不要加strless()
 97     myAset.insert("323");// 比较字符串大小
 98     myAset.insert("123");
 99     myAset.insert("223");
100     for(auto i : myAset)
101     {
102         cout << i << endl;
103     }
104 
105     cin.get();
106 }
107 
108 //------------------------------------------------------------------------
109 
110 
111 // multiset
112 
113 #include<iostream>
114 #include<set>
115 #include<cstring>
116 
117 using namespace std;
118 
119 void main()
120 {
121     multiset<int> myset;
122     myset.insert(1);
123     myset.insert(13);
124     myset.insert(11);
125     myset.insert(12);
126     myset.insert(19);
127     myset.insert(19);
128     myset.insert(19);
129     myset.insert(19);
130     myset.insert(13);
131     myset.insert(13);
132 
133     for(auto i : myset)
134     {
135         cout << i << endl;    
136     }
137     
138     cout << "\n\n\n";
139 
140     auto it = myset.equal_range(13);// 返回一个链表的指针
141     // it.first就是第一个迭代器等价于begin,it.second()就是最后一个为空
142     for(auto i = it.first;i!=it.second();i++)
143     {
144         cout << *i << endl;    
145     }
146     
147 
148     cin.get();
149 }

 

posted on 2015-06-14 16:57  Dragon-wuxl  阅读(283)  评论(0)    收藏  举报

导航