C++之set/multise容器
set基本概念
set构造和赋值
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{set<int> s1;//插入数据,只有insert方式s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(40);s1.insert(30);//set容器特点: 所有元素插入时候自动被排序//set容器不允许插入重复值PrintfSet(s1);//拷贝构造set<int>s2(s1);PrintfSet(s2);//赋值操作set<int>s3;s3 = s1;PrintfSet(s3);
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set大小和交换
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{set<int> s1;//插入数据,只有insert方式s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(40);s1.insert(30);//set容器特点: 所有元素插入时候自动被排序//set容器不允许插入重复值PrintfSet(s1);if(s1.empty()){cout<<"s1 is empty"<<endl;}else{cout<<"s1 is not empty"<<endl;cout<<"s1's size is "<<s1.size()<<endl;}set<int>s2;s2.insert(100);s2.insert(200);s2.insert(300);s2.insert(400);//swapcout<<"before swap"<<endl;PrintfSet(s1);PrintfSet(s2);s2.swap(s1);cout<<"after swap"<<endl;PrintfSet(s1);PrintfSet(s2);
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set插入和删除
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{set<int> s1;//插入数据,只有insert方式s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(40);s1.insert(30);//set容器特点: 所有元素插入时候自动被排序//set容器不允许插入重复值PrintfSet(s1);s1.erase(s1.begin());PrintfSet(s1);s1.erase(30);PrintfSet(s1);//s1.erase(s1.begin(),s1.end());s1.clear();}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set查找和统计
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{set<int> s1;//插入数据,只有insert方式s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(40);s1.insert(30);//set容器特点: 所有元素插入时候自动被排序//set容器不允许插入重复值PrintfSet(s1);set<int>::iterator pos = s1.find(30);if(pos != s1.end()){cout<<"have finded"<<endl;}else{cout<<"no find"<<endl;}
//对于set而言 统计结果 要么是0 要么是1int num = s1.count(30);cout<<"num is "<<num<<endl;
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set与multise区别
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{set<int> s1;//插入数据,只有insert方式pair<set<int>::iterator,bool> ret = s1.insert(10);if(ret.second){cout<<"insert success at first"<<endl;}else{cout<<"insert fail at first"<<endl;}ret = s1.insert(10);if(ret.second){cout<<"insert success at second"<<endl;}else{cout<<"insert fail at second"<<endl;}multiset<int>m1;//允许插入重复值m1.insert(10);m1.insert(10);m1.insert(10);m1.insert(10);for(multiset<int>::iterator it = m1.begin();it != m1.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
pair对组的创建
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}void test()
{//第一种方式pair<string,int>p("Tom",20);cout<<"name is "<<p.first<<"age is "<<p.second<<endl;//第二种方式pair<string,int>p2 = make_pair("Jin",21);cout<<"name is "<<p2.first<<"age is "<<p2.second<<endl;
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set排序
#include <iostream>
#include<set>
using namespace std;void PrintfSet(set<int>&s)
{for(set<int>::iterator it = s.begin();it != s.end();it++){cout<<*it<<" ";}cout<<endl;
}
class MyCompare
{
public:bool operator()(int v1,int v2){return v1>v2;}
};void test()
{set<int>s1;s1.insert(10);s1.insert(40);s1.insert(30);s1.insert(50);s1.insert(20);PrintfSet(s1);//指定排序规则为从大到小set<int,MyCompare>s2;s2.insert(10);s2.insert(40);s2.insert(30);s2.insert(50);s2.insert(20);for(set<int,MyCompare>::iterator it = s2.begin();it != s2.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();cout << "Hello World!" << endl;return 0;
}
set存放自定义数据
#include<iostream>
#include<string>
using namespace std;
#include<set>
//set容器排序,存放自定义数据类型
class Person
{
public:Person(string name,int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};class MyCompare
{
public:bool operator()(const Person&p1, const Person& p2){//按照年龄进行降序return p1.m_age > p2.m_age;}
};void test()
{//指定排序规则为从大到小set<Person, MyCompare>s;//创建Person对象Person p1 ("liubei",23);Person p2("zhangfei", 21);Person p3("guanyu", 22);Person p4("zhaoyun", 24);s.insert(p1);s.insert(p2);s.insert(p3);s.insert(p4);for (set<Person, MyCompare>::iterator it = s.begin();it != s.end();it++){cout << "name is " << (*it).m_name << "age is " << (*it).m_age << endl;}cout << endl;
}int main()
{test();system("pause");return 0;
}