STL容器之set

时间:2023-03-08 21:24:27
STL容器之set

【1】set容器

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

【2】set容器方法

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

应用示例代码如下:

 #include <set>
#include <iostream>
using namespace std; bool funcComp(int lhs, int rhs)
{
return lhs > rhs;
} struct classcomp
{
bool operator() (const int& lhs, const int& rhs) const
{
return lhs < rhs;
}
};
// 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
for (; iter != lessSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
}
// 反向遍历
void print(const set<int, greater<int>> & greaterSet)
{
set<int, greater<int>>::reverse_iterator ritor;
ritor = greaterSet.rbegin();
while (ritor != greaterSet.rend())
{
cout << (*ritor) << " ";
++ritor;
}
cout << endl;
} void print(const set<int, bool(*)(int, int)> & funcpSet)
{
set<int, bool(*)(int, int)>::iterator iter = funcpSet.begin();
for (; iter != funcpSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
} void print(const set<int, classcomp> & classSet)
{
set<int, classcomp>::iterator iter = classSet.begin();
for (; iter != classSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
} void main ()
{
// 1.默认构造函数创建一个空的set容器
set<int> first;
int n = ;
while (n <= )
{
first.insert(n++);
}
cout << "打印first容器的值:" << endl;
print(first); // 2.默认是以小于比较器less<int>创建的,再创建一个带大于比较器的set
set<int, greater<int>> second;
n = ;
while (n <= )
{
second.insert(n++);
}
cout << "打印second容器的值:" << endl;
print(second); // 3.用数组元素值创建一个容器
int myInts[] = {, , , , };
set<int> third(myInts, myInts + );
cout << "打印third容器的值:" << endl;
print(third); // 4.调用拷贝构造函数创建一个容器
set<int> fourth(third);
cout << "打印fourth容器的值:" << endl;
print(fourth); // 5.由已知对象的区间创建一个容器
set<int> fifth(first.begin(), first.end());
cout << "打印fifth容器的值:" << endl;
print(fifth); // 6.以函数指针为比较器
bool(*func_pt)(int, int) = funcComp;
set<int, bool(*)(int, int)> sixth(func_pt);
for (int i = ; i < ; ++i)
{
sixth.insert(rand() % );
}
cout << "打印sixth容器的值:" << endl;
print(sixth); // 7.以仿函数为比较器
set<int, classcomp> seventh;
for (int i = ; i < ; ++i)
{
seventh.insert(rand() % );
}
cout << "打印seventh容器的值:" << endl;
print(seventh); system("pause");
} // run out:
/*
打印first容器的值:
1 2 3 4 5 6 7 8 9 10
打印second容器的值:
10 11 12 13 14 15 16 17 18 19 20
打印third容器的值:
10 20 30 40 50
打印fourth容器的值:
10 20 30 40 50
打印fifth容器的值:
1 2 3 4 5 6 7 8 9 10
打印sixth容器的值:
78 69 67 64 62 58 41 34 24 0
打印seventh容器的值:
5 27 36 42 45 61 81 91 95
请按任意键继续. . .
*/

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter) << " ";
iter++;
}
cout << endl;
} void main()
{
set<int> myset;
set<int>::iterator it;
pair<set<int>::iterator, bool> ret;
// 插入数据元素
for (int i = ; i <= ; ++i)
{
myset.insert(i * ); // 元素为: 10 20 30 40 50
} ret = myset.insert(); // 再插入20,发现已存在,则插入操作失败!
if (false == ret.second)
it = ret.first; // it迭代器指向了20这个元素 myset.insert (it, );
myset.insert (it, );
myset.insert (it, ); int myints[] = {, , }; // 10已经在容器中
myset.insert(myints, myints + );
cout << "打印mySet容器的值:" << endl;
print(myset); set<int> firstSet;
for (int i = ; i < ; ++i)
{
firstSet.insert(rand() % );
}
cout << "打印firstSet容器的值:" << endl;
print(firstSet); cout << "empty():" << firstSet.empty() << endl;
cout << "size():" << firstSet.size() << endl;
cout << "max_size():" << firstSet.max_size() << endl; system("pause");
} // run out:
/*
打印mySet容器的值:
5 10 15 20 24 25 26 30 40 50
打印firstSet容器的值:
0 24 34 41 58 62 64 67 69 78
empty():0
size():10
max_size():1073741823
请按任意键继续. . .
*/

(3)删除、清空、交换

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter) << " ";
iter++;
}
cout << endl;
} void main ()
{
set<int> mySet;
set<int>::iterator it;
for (int i = ; i < ; i++)
mySet.insert(i * ); // 10 20 30 40 50 60 70 80 90
cout << "打印mySet容器数据:" << endl;
print(mySet); it = mySet.begin();
++it;
// 第一种删除方式
mySet.erase(it);
// 第二种删除方式
mySet.erase();
it = mySet.find();
// 第三种删除方式
mySet.erase(it, mySet.end());
cout << "删除后,打印mySet容器数据:" << endl;
print(mySet); set<int> firstSet;
for (int i = ; i < ; ++i)
{
firstSet.insert((i + ) * );
}
cout << "打印firstSet容器的值:" << endl;
print(firstSet);
// 第四种删除
set<int>::iterator iter = firstSet.begin();
for (; iter != firstSet.end(); )
{
if ((*iter) == )
{
firstSet.erase(iter++);
}
else
{
++iter;
}
}
cout << "删除50后,打印firstSet容器的值:" << endl;
print(firstSet); mySet.clear(); // 清空
firstSet.swap(mySet); // 交换两个容器
cout << "交换后,打印mySet容器的值:" << endl;
print(mySet);
cout << "交换后,打印firstSet容器的值:" << endl;
print(firstSet); system("pause");
} //run out:
/*
打印mySet容器数据:
10 20 30 40 50 60 70 80 90
删除后,打印mySet容器数据:
10 30 50
打印firstSet容器的值:
30 40 50 60 70 80 90 100 110
删除50后,打印firstSet容器的值:
30 40 60 70 80 90 100 110
交换后,打印mySet容器的值:
30 40 60 70 80 90 100 110
交换后,打印firstSet容器的值: 请按任意键继续. . .
*/

(4)key_comp函数

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter++) << " ";
}
cout << endl;
} void main ()
{
set<int> mySet;
int highest;
set<int>::key_compare myComp = mySet.key_comp();
for (int i = ; i <= ; ++i)
{
mySet.insert((i + ) * );
} cout << "打印mySet容器中的数据元素:" << endl;
print(mySet); cout << "利用比较函数打印容器中小于最大值的元素:" << endl;
highest = *mySet.rbegin();
set<int>::iterator it = mySet.begin();
do
{
cout << " " << *it;
} while (myComp(*(++it), highest));
cout << endl; system("pause");
} // run out:
/*
打印mySet容器中的数据元素:
10 20 30 40 50 60
利用比较函数打印容器中小于最大值的元素:
10 20 30 40 50
请按任意键继续. . .
*/

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
// set some initial values:
for (int i = ; i < ; ++i)
mySet.insert(i * ); // set: 3 6 9 12 if (mySet.count() == )
cout << " 9 is an element of myset.\n";
else
cout << " 9 is not an element of myset.\n"; system("pause");
}
// run out:
/*
9 is an element of myset.
请按任意键继续. . .
*/

(6)lower_bound 和 upper_bound函数

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

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
set<int>::iterator itlow, itup;
for (int i = ; i < ; i++)
mySet.insert(i * ); // 10 20 30 40 50 60 70 80 90 itlow = mySet.lower_bound ();
itup = mySet.upper_bound ();
mySet.erase(itlow, itup); // 10 20 70 80 90 cout << "mySet contains:";
for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it)
{
cout << " " << *it;
} cout << endl; system("pause");
}
// run out:
/*
mySet contains: 10 20 70 80 90
请按任意键继续. . .
*/

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
for (int i = ; i <= ; ++i)
mySet.insert(i * ); // mySet: 10 20 30 40 50 pair<set<int>::const_iterator, set<int>::const_iterator> ret;
ret = mySet.equal_range(); cout << "the lower bound points to: " << (*ret).first << endl;
cout << "the upper bound points to: " << (*ret).second << endl; system("pause");
} // run out:
/*
the lower bound points to: 30
the upper bound points to: 40
请按任意键继续. . .
*/

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

示例代码如下:

 #include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> myset;
int* p = NULL;
unsigned int i; // allocate an array of 5 elements using myset's allocator:
p = myset.get_allocator().allocate(); // assign some values to array
for (i = ; i < ; ++i)
p[i] = (i + ) * ; cout << "The allocated array contains:";
for (i = ; i < ; ++i)
cout << ' ' << p[i]; cout << endl; myset.get_allocator().deallocate(p, ); system("pause");
} // run out:
/*
The allocated array contains: 10 20 30 40 50
请按任意键继续. . .
*/

(9)待续......

【3】set容器总结

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

Good Good  Study,  Day  Day  Up.

顺序   选择   循环   总结