STL容器之set

【1】set容器

一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。

【2】set容器方法

(1)set构造函数、插入函数、遍历过程

应用示例代码如下:

  1 #include <set>
  2 #include <iostream>
  3 using namespace std;
  4 
  5 bool funcComp(int lhs, int rhs)  
  6 {
  7     return lhs > rhs;
  8 }
  9 
 10 struct classcomp
 11 {
 12     bool operator() (const int& lhs, const int& rhs) const
 13     {
 14         return lhs < rhs;
 15     }
 16 };
 17 // 正向遍历
 18 void print(const set<int> & lessSet)
 19 {
 20     set<int>::iterator iter = lessSet.begin();
 21     for (; iter != lessSet.end(); ++iter)
 22     {
 23         cout << (*iter) << " ";
 24     }
 25     cout << endl;
 26 }
 27 // 反向遍历
 28 void print(const set<int, greater<int>> & greaterSet)
 29 {
 30     set<int, greater<int>>::reverse_iterator ritor;
 31     ritor = greaterSet.rbegin();
 32     while (ritor != greaterSet.rend())
 33     {
 34         cout << (*ritor) << " ";
 35         ++ritor;
 36     }
 37     cout << endl;
 38 }
 39 
 40 void print(const set<int, bool(*)(int, int)> & funcpSet)
 41 {
 42     set<int, bool(*)(int, int)>::iterator iter = funcpSet.begin();
 43     for (; iter != funcpSet.end(); ++iter)
 44     {
 45         cout << (*iter) << " ";
 46     }
 47     cout << endl;
 48 }
 49 
 50 void print(const set<int, classcomp> & classSet)
 51 {
 52     set<int, classcomp>::iterator iter = classSet.begin();
 53     for (; iter != classSet.end(); ++iter)
 54     {
 55         cout << (*iter) << " ";
 56     }
 57     cout << endl;
 58 }
 59 
 60 void main ()
 61 {
 62     // 1.默认构造函数创建一个空的set容器
 63     set<int> first;
 64     int n = 1;
 65     while (n <= 10)
 66     {
 67         first.insert(n++);
 68     }
 69     cout << "打印first容器的值:" << endl;
 70     print(first);
 71   
 72     // 2.默认是以小于比较器less<int>创建的,再创建一个带大于比较器的set
 73     set<int, greater<int>> second;
 74     n = 10;
 75     while (n <= 20)
 76     {
 77         second.insert(n++);
 78     }
 79     cout << "打印second容器的值:" << endl;
 80     print(second);
 81 
 82     // 3.用数组元素值创建一个容器
 83     int myInts[] = {10, 20, 30, 40, 50};
 84     set<int> third(myInts, myInts + 5);
 85     cout << "打印third容器的值:" << endl;
 86     print(third);
 87 
 88     // 4.调用拷贝构造函数创建一个容器
 89     set<int> fourth(third);
 90     cout << "打印fourth容器的值:" << endl;
 91     print(fourth);
 92 
 93     // 5.由已知对象的区间创建一个容器
 94     set<int> fifth(first.begin(), first.end());
 95     cout << "打印fifth容器的值:" << endl;
 96     print(fifth);
 97 
 98     // 6.以函数指针为比较器
 99     bool(*func_pt)(int, int) = funcComp;
100     set<int, bool(*)(int, int)> sixth(func_pt);
101     for (int i = 0; i < 10; ++i)
102     {
103         sixth.insert(rand() % 100);
104     }
105     cout << "打印sixth容器的值:" << endl;
106     print(sixth);
107 
108     // 7.以仿函数为比较器
109     set<int, classcomp> seventh;
110     for (int i = 0; i < 10; ++i)
111     {
112         seventh.insert(rand() % 100);
113     }
114     cout << "打印seventh容器的值:" << endl;
115     print(seventh);
116 
117     system("pause");
118 }
119 
120 // run out:
121 /*
122 打印first容器的值:
123 1 2 3 4 5 6 7 8 9 10
124 打印second容器的值:
125 10 11 12 13 14 15 16 17 18 19 20
126 打印third容器的值:
127 10 20 30 40 50
128 打印fourth容器的值:
129 10 20 30 40 50
130 打印fifth容器的值:
131 1 2 3 4 5 6 7 8 9 10
132 打印sixth容器的值:
133 78 69 67 64 62 58 41 34 24 0
134 打印seventh容器的值:
135 5 27 36 42 45 61 81 91 95
136 请按任意键继续. . .
137 */

(2)插入、大小、判空、最大个数等等

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 // 正向遍历
 6 void print(const set<int> & lessSet)
 7 {
 8     set<int>::iterator iter = lessSet.begin();
 9     while (iter != lessSet.end())
10     {
11         cout << (*iter) << " ";
12         iter++;
13     }
14     cout << endl;
15 }
16 
17 void main()
18 {
19     set<int> myset;
20     set<int>::iterator it;
21     pair<set<int>::iterator, bool> ret;
22     // 插入数据元素
23     for (int i = 1; i <= 5; ++i) 
24     {
25         myset.insert(i * 10);    // 元素为: 10 20 30 40 50
26     }
27 
28     ret = myset.insert(20);  // 再插入20,发现已存在,则插入操作失败!
29     if (false == ret.second) 
30         it = ret.first;  // it迭代器指向了20这个元素
31 
32     myset.insert (it, 25);
33     myset.insert (it, 24);
34     myset.insert (it, 26);
35 
36     int myints[] = {5, 10, 15};  // 10已经在容器中
37     myset.insert(myints, myints + 3);
38     cout << "打印mySet容器的值:" << endl;
39     print(myset);
40 
41     set<int> firstSet;
42     for (int i = 0; i < 10; ++i)
43     {
44         firstSet.insert(rand() % 100);
45     }
46     cout << "打印firstSet容器的值:" << endl;
47     print(firstSet);
48 
49     cout << "empty():" << firstSet.empty() << endl; 
50     cout << "size():" << firstSet.size() << endl;
51     cout << "max_size():" << firstSet.max_size() << endl;
52     
53     system("pause");
54 }
55 
56 // run out:
57 /*
58 打印mySet容器的值:
59 5 10 15 20 24 25 26 30 40 50
60 打印firstSet容器的值:
61 0 24 34 41 58 62 64 67 69 78
62 empty():0
63 size():10
64 max_size():1073741823
65 请按任意键继续. . .
66 */

(3)删除、清空、交换

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 // 正向遍历
 6 void print(const set<int> & lessSet)
 7 {
 8     set<int>::iterator iter = lessSet.begin();
 9     while (iter != lessSet.end())
10     {
11         cout << (*iter) << " ";
12         iter++;
13     }
14     cout << endl;
15 }
16 
17 void main ()
18 {
19     set<int> mySet;
20     set<int>::iterator it;
21     for (int i = 1; i < 10; i++) 
22         mySet.insert(i * 10);  // 10 20 30 40 50 60 70 80 90
23     cout << "打印mySet容器数据:" << endl;
24     print(mySet);
25 
26     it = mySet.begin();
27     ++it;
28     // 第一种删除方式
29     mySet.erase(it);
30     // 第二种删除方式
31     mySet.erase(40);
32     it = mySet.find(60);
33     // 第三种删除方式
34     mySet.erase(it, mySet.end());
35     cout << "删除后,打印mySet容器数据:" << endl;
36     print(mySet);
37 
38     set<int> firstSet;
39     for (int i = 1; i < 10; ++i)
40     {
41         firstSet.insert((i + 2) * 10);
42     }
43     cout << "打印firstSet容器的值:" << endl;
44     print(firstSet);
45     // 第四种删除
46     set<int>::iterator iter = firstSet.begin();
47     for (; iter != firstSet.end(); )
48     {
49         if ((*iter) == 50)
50         {
51             firstSet.erase(iter++);
52         }
53         else
54         {
55             ++iter;
56         }
57     }
58     cout << "删除50后,打印firstSet容器的值:" << endl;
59     print(firstSet);
60 
61     mySet.clear();  // 清空
62     firstSet.swap(mySet); // 交换两个容器
63     cout << "交换后,打印mySet容器的值:" << endl;
64     print(mySet);
65     cout << "交换后,打印firstSet容器的值:" << endl;
66     print(firstSet);
67 
68     system("pause");
69 }
70 
71 //run out:
72 /*
73 打印mySet容器数据:
74 10 20 30 40 50 60 70 80 90
75 删除后,打印mySet容器数据:
76 10 30 50
77 打印firstSet容器的值:
78 30 40 50 60 70 80 90 100 110
79 删除50后,打印firstSet容器的值:
80 30 40 60 70 80 90 100 110
81 交换后,打印mySet容器的值:
82 30 40 60 70 80 90 100 110
83 交换后,打印firstSet容器的值:
84 
85 请按任意键继续. . .
86 */

(4)key_comp函数

函数返回比较函数对象,默认的是升序排列。

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 // 正向遍历
 6 void print(const set<int> & lessSet)
 7 {
 8     set<int>::iterator iter = lessSet.begin();
 9     while (iter != lessSet.end())
10     {
11         cout << (*iter++) << " ";
12     }
13     cout << endl;
14 }
15 
16 void main ()
17 {
18   set<int> mySet;
19   int highest;
20   set<int>::key_compare myComp = mySet.key_comp();
21   for (int i = 0; i <= 5; ++i) 
22   {
23       mySet.insert((i + 1) * 10);
24   }
25 
26   cout << "打印mySet容器中的数据元素:" << endl;
27   print(mySet);
28 
29   cout << "利用比较函数打印容器中小于最大值的元素:" << endl;
30   highest = *mySet.rbegin();
31   set<int>::iterator it = mySet.begin();
32   do 
33   {
34       cout << " " << *it;
35   } while (myComp(*(++it), highest));
36   cout << endl;
37 
38   system("pause");
39 }
40 
41 // run out:
42 /*
43 打印mySet容器中的数据元素:
44 10 20 30 40 50 60
45 利用比较函数打印容器中小于最大值的元素:
46  10 20 30 40 50
47 请按任意键继续. . .
48 */

(5)count函数。函数返回值为val的元素的个数,当然在set容器中其要么为0,要么为1。

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void main ()
 6 {
 7     set<int> mySet;
 8     // set some initial values:
 9     for (int i = 1; i < 5; ++i) 
10         mySet.insert(i * 3);    // set: 3 6 9 12
11 
12     if (mySet.count(9) == 1)
13         cout << " 9 is an element of myset.\n";
14     else
15         cout << " 9 is not an element of myset.\n";
16     
17     system("pause");
18 }
19 // run out:
20 /*
21  9 is an element of myset.
22 请按任意键继续. . .
23 */

(6)lower_bound 和 upper_bound函数

lower_bound 函数返回set中第一个小于或者等于val的元素的iterator。

upper_bound 函数返回set中第一个大于或者等于val的元素的iterator。

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void main ()
 6 {
 7     set<int> mySet;
 8     set<int>::iterator itlow, itup;
 9     for (int i = 1; i < 10; i++) 
10         mySet.insert(i * 10); // 10 20 30 40 50 60 70 80 90
11 
12     itlow = mySet.lower_bound (30);
13     itup = mySet.upper_bound (60); 
14     mySet.erase(itlow, itup);  // 10 20 70 80 90
15 
16     cout << "mySet contains:";
17     for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it)
18     {
19         cout << " " << *it;
20     }
21     
22     cout << endl;
23     
24     system("pause");
25 }
26 // run out:
27 /*
28 mySet contains: 10 20 70 80 90
29 请按任意键继续. . .
30 */

(7)equal_range 函数返回等于set中val的上下界的iterator。

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void main ()
 6 {
 7     set<int> mySet;
 8     for (int i = 1; i <= 5; ++i) 
 9         mySet.insert(i * 10);   // mySet: 10 20 30 40 50
10   
11     pair<set<int>::const_iterator, set<int>::const_iterator> ret;
12     ret = mySet.equal_range(30);
13 
14     cout << "the lower bound points to: " << (*ret).first << endl;
15     cout << "the upper bound points to: " << (*ret).second << endl;
16 
17     system("pause");
18 }
19 
20 // run out:
21 /*
22 the lower bound points to: 30
23 the upper bound points to: 40
24 请按任意键继续. . .
25 */

(8)get_allocator 函数返回set的分配器对象。

示例代码如下:

 1 #include <set>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void main ()
 6 {
 7     set<int> myset;
 8     int* p = NULL;
 9     unsigned int i;
10 
11     // allocate an array of 5 elements using myset's allocator:
12     p = myset.get_allocator().allocate(5);
13 
14     // assign some values to array
15     for (i = 0; i < 5; ++i) 
16         p[i] = (i + 1) * 10;
17 
18     cout << "The allocated array contains:";
19     for (i = 0; i < 5; ++i) 
20         cout << ' ' << p[i];
21 
22     cout << endl;
23 
24     myset.get_allocator().deallocate(p, 5);
25 
26     system("pause");
27 }
28 
29 // run out:
30 /*
31 The allocated array contains: 10 20 30 40 50
32 请按任意键继续. . .
33 */

(9)待续......

【3】set容器总结

 一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。

 

Good Good  Study,  Day  Day  Up.

顺序   选择   循环   总结

posted @ 2017-02-21 23:55  kaizenly  阅读(3457)  评论(0编辑  收藏  举报
打赏