1、set是一個集合容器,其所包含的元素是唯一的,元素按順序排列,不支持在指定位置插入元素,不可以直接存取元素。
2、multiset與set的區別:set容器中元素唯一,而multiset支持中支持同一值出現多次。
3、不可以直接修改set或multiset容器中的元素值,因爲該類容器是自動排序的。如果希望修改一個元素值,必須先刪除原有的元素,再插入新的元素。
- #include <set>
set的插入與迭代器
- set.insert(elem); //在容器中插入元素。
- set.begin(); //返回容器中第一個數據的迭代器。
- set.end(); //返回容器中最後一個數據之後的迭代器。
- set.rbegin(); //返回容器中倒數第一個元素的迭代器。
- set.rend(); //返回容器中倒數最後一個元素的後面的迭代器。
set集合的元素排序
- set<int,less<int> > setIntA; //該容器是按升序方式排列元素。
- set<int,greater<int>> setIntB; //該容器是按降序方式排列元素。
- set<int> 相當於 set<int,less<int>>
set對象的拷貝構造與賦值
- set(const set &st); //拷貝構造函數
- set& operator=(const set &st); //重載等號操作符
- set.swap(st); //交換兩個集合容器
set的大小
- set.size(); //返回容器中元素的數目
- set.empty(); //判斷容器是否爲空
set的刪除
- set.clear(); //清除所有元素
- set.erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
- set.erase(beg,end); //刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
- set.erase(elem); //刪除容器中值爲elem的元素。
set的查找
- set.find(elem); //查找elem元素,返回指向elem元素的迭代器。
- set.count(elem); //返回容器中值爲elem的元素個數。對set來說只存在1個或0個。對multiset來說,值可能大於1。
- set.lower_bound(elem); //返回第一個>=elem元素的迭代器。
- set.upper_bound(elem); //返回第一個>elem元素的迭代器。
- set.equal_range(elem); //返回容器中與elem相等的上下限的兩個迭代器。上限是閉區間,下限是開區間,如[beg,end)
pair的使用
- pair譯爲對組,可以將兩個值視爲一個單元。
- pair<T1,T2>存放的兩個值的類型,可以不一樣,如T1爲int,T2爲float。T1,T2也可以是自定義類型。
- pair.first是pair裏面的第一個值,是T1類型。
- pair.second是pair裏面的第二個值,是T2類型。
set基本用法
void Fun1()
{
set<int> s1;
//set<int,less<int>> s1;//升序
//set<int, greater<int>> s1;//降序
/*cout << "輸入一組數,輸入的值不能重複出現:" << endl;
int num = 0;
scanf("%d", &num);
while (num != 0)
{
s1.insert(num);
scanf("%d", &num);
}*/
for (int i = 0; i <10;i++)
{
s1.insert(rand());
}
cout << "插入到set集合後得到的結果:(默認爲升序)" << endl;
for (set<int>::iterator it = s1.begin(); it != s1.end();it++ )
{
cout << *it << " ";
}
cout << endl;
cout << "刪除" << endl;
set<int, less<int>>::iterator it = s1.begin();
it++;
it++;
s1.erase(it);
for (set<int, less<int>>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int> s2;
for (int i = 0; i < 10; i++)
{
s2.insert(i+1);
}
cout << "查找" << endl;
set<int>::iterator it1 = s2.find(5);
cout << *it1 << endl;
int count = s2.count(5);
cout <<"元素5的個數:"<< count << endl;
set<int>::iterator it2;
it2 = s2.lower_bound(5);
cout <<"返回第一個>=5的元素:"<< *it2 << endl;
it2 = s2.upper_bound(5);
cout << "返回第一個>5的元素:" << *it2 << endl;
cout << endl;
//using _Pairii = pair<iterator, iterator>;
pair<set<int>::iterator, set<int>::iterator> pairit = s2.equal_range(5);
cout << "輸出與元素5相等的上下限" << endl;
set<int>::iterator it3 = pairit.first;
cout << "上限:" << *it3 << endl;
set<int>::iterator it4 = pairit.second;
cout << "下限:" << *it4 << endl;
}
函數對象functor的用法(仿函數)
設想如果set集合裏面插入一個班級的考試成績,想得到成績最大到小排列
相當於簡易版的greater<int>函數對象
容器就是調用函數對象的operator()方法去比較兩個值的大小。
class Student
{
public:
Student(const int grade, const char *name)
{
this->m_grade = grade;
strcpy(m_name, name);
}
public:
int m_grade;
char m_name[64];
};
struct RankFun
{
bool operator()(const Student &iStu1,const Student &iStu2)
{
return(iStu1.m_grade > iStu2.m_grade);
}
};
void Fun2()
{
multiset<Student, RankFun> sStu;
sStu.insert(Student(85, "小馮"));
sStu.insert(Student(81, "小張"));
sStu.insert(Student(90, "小徐"));
sStu.insert(Student(95, "小尹"));
sStu.insert(Student(88, "小廖"));
sStu.insert(Student(80, "小吳"));
sStu.insert(Student(90, "小龔"));
sStu.insert(Student(95, "小王"));
sStu.insert(Student(85, "小李"));
cout << "按成績排名" << endl;
for (multiset<Student, RankFun>::iterator it = sStu.begin();it != sStu.end();it++)
{
cout << it->m_name << ": " << it->m_grade << endl;
}
}
總結:
一、容器set/multiset的使用方法;
紅黑樹的變體,查找效率高,插入不能指定位置,插入時自動排序。
二、functor的使用方法;
類似於函數的功能,可用來自定義一些規則,如元素比較規則。
三、pair的使用方法。
對組,一個整體的單元,存放兩個類型(T1,T2,T1可與T2一樣)的兩個元素。