C++ STL set

tech2022-08-11  129

set/multiset set:所有元素都会在插入时自动被排序; set/multiset属于关联式容器,底层结构是用二叉树实现;

size(); //set容器的大小 empty(); //set容器是否为空 swap(); //交换两个容器

set函数 1.insert(elem); //set容器中插入元素 2.clear(); //清除set容器中所有元素 3.erase(pos); //删除pos迭代器所指元素 ,返回下一个元素的迭代器 4.erase(begin, end); //删除beg,end区间 的所有元素,返回下一个元素的迭代器 5.erase(elem); //删除容器中值为elem的元素

find(elem);//查找容器中的元素是否存在 如果存在返回所在迭代器,不存在返回set.end()

count(elem);//返回指定元素个数 set和multiset区别 set不可插入重复值,multiset可以 set插入数据会返回插入结果,标识插入是否成功 multiset不会检测插入结果,所以可以插入重复数据

对组创建,frist、second获取对组元素值 pair<type, type> p(value, value); pair<type, type> p=make_pair(value, value);

#include <iostream> #include <string> #include <deque> #include <algorithm> #include <list> #include <set> using namespace std; void printset(const set<int> &st) { for (set<int>::const_iterator it=st.begin(); it != st.end(); it++) { cout<<*it<<" "; } cout<<endl; } void printmultiset(const multiset<int> &st) { for (multiset<int>::const_iterator it=st.begin(); it != st.end(); it++) { cout<<*it<<" "; } cout<<endl; } //set void test1() { set<int> st; //set容器只有insert函数添加元素方法 st.insert(20); st.insert(50); st.insert(30); pair<set<int>::iterator, bool> ret = st.insert(20); //插入重复值,会失败 if (ret.second) { cout<<"插入数据成功"<<endl; } else { cout<<"插入失败"<<endl; } st.insert(40); printset(st);//发现自动排序并去重 set<int> st1(st); //删除set容器中指定的元素值 st1.erase(20); printset(st1); set<int> st2=st1; printset(st2); } //multiset void test2() { multiset<int> st; //set容器只有insert函数添加元素方法 st.insert(20); st.insert(50); st.insert(30); st.insert(20); st.insert(40); printmultiset(st);//发现自动排序但是并不去重复元素 multiset<int> st1(st); //删除set容器中指定的元素值 st1.erase(20); printmultiset(st1); //删除时,相同的元素值都被删除吊 multiset<int> st2=st1; printmultiset(st2); } //size统计容器元素个数 void test3() { set<int> st; st.insert(20); st.insert(50); st.insert(30); st.insert(20); st.insert(40); cout<<"size:"<<st.size()<<endl; set<int> st1; st1.insert(90); st1.insert(100); cout<<"size:"<<st1.size()<<endl; st.swap(st1); //交换两个容器 printset(st); printset(st1); } //rease删除重载函数和clear清除函数 void test4() { set<int> st; st.insert(20); st.insert(50); st.insert(30); st.insert(20); st.insert(40); //erase删除区间 set<int> st1(st); st1.erase(st1.begin(), st1.end()); printset(st1); //清除set容器中的所有元素 set<int> st2=st; st2.clear(); printset(st1); } //find寻找元素和count统计函数 void test5() { set<int> st; st.insert(20); st.insert(50); st.insert(30); st.insert(20); st.insert(40); //只要元素不存在,就返回end迭代器 set<int>::iterator pos = st.find(20); if (pos != st.end()) { cout<<"找到:"<<*pos<<endl; } else { cout<<"未找到"<<endl; } //针对set容器而言,统计结果要么是0或1(set容器会对元素去重) cout<<"元素个数:"<<st.count(50)<<endl; } //pair对组 void test6() { pair<string, int> p("zhangsan", 20); cout<<"name:"<<p.first<<" "<<"age:"<<p.second<<endl; pair<string, int> p1 = make_pair("lisi", 50); cout<<"name:"<<p1.first<<" "<<"age:"<<p1.second<<endl; } //set容器自定义数据类型排序 class Person { public: Person(string name, int age):m_name(name), m_age(age){} string m_name; int m_age; }; class ComparePerson { public: bool operator()(const Person &p1, const Person &p2) { return p1.m_age<p2.m_age; } }; void test7() { set<Person, ComparePerson> st; Person p1("刘备", 40); Person p2("关羽", 38); Person p3("张飞", 36); Person p4("赵云", 30); st.insert(p1); st.insert(p2); st.insert(p3); st.insert(p4); for (set<Person, ComparePerson>::iterator it=st.begin(); it!=st.end(); it++) { cout<<"name:"<<it->m_name<<" "<<"age:"<<it->m_age<<endl; } } int main() { //test1(); //test2(); test3(); return 0; }
最新回复(0)