一、set
1. set基本概念
简介:
 所有元素都会在插入时自动被排序
 本质:
 set/multiset属于关联式容器,底层结构是用二叉树实现。
 set和multiset区别:
 set不允许容器中有重复的元素
multiset允许容器中有重复的元素
2. set构造和赋值
构造:
 set<T> st;                  //默认构造函数:
 set(const set &st);     //拷贝构造函教
赋值:
 set& operator=(onst set &st);       //重载等号操作符
//打印
 void PrintSet(const set<int>& s) {
     for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
         cout << *it << "  ";
     }
     cout << endl;
 }
 void test494533()
 {
     set<int> s1;
     //插入数据只有insert方式
     // set容器特点:所有元素插入时候自动被排序
     // set容器不允许插入重复值
     s1.insert(123);
     s1.insert(25);
     s1.insert(33);
     s1.insert(63);
     //打印
     PrintSet(s1);
    //拷贝构造
     set<int> s2(s1);
     PrintSet(s2);
    set<int> s3;
     s3 = s2;
     PrintSet(s3);
 }
3. set大小和交换
函数原型:
 size();                    //返回容器中元素的数目
 empty();              //判断容器是否为空
 swap(st);               //交换两个集合容器
4. set插入和删除
函数原型:
 insert(elem);          //在容器中插入元素。
 clear();                   //清除所有元素
 erase(pos);              //删除pos迭代器所指的元素,返回下一个元素的迭代器。

 erase(beg,end);       //删除区间[beg,end)的所有元素,返回下一个元素的迭代器。
 erase(elem);            //删除容器中值为elem的元素。
5. set查找和统计
函数原型:
 find(key);      //查找key是否存在,若存在,返回该键的元素的迭代器; 若不存在,返回set.end();
 count( key);   //统计key的元素个数  0 1

6.set和multiset区别
区别:
 set不可以插入重复数据,而multiset可以
 set插入数据的同时会返回插入结果,表示插入是否成功.

multiset不会检测数据,因此可以插入重复数据
7.pair对组创建
功能描述:
 成对出现的数据,利用对组可以返回两个薮据
两种创建方式:
 pair<type, type> p ( value1, value2 );
 pair<type, type> p -=make_pair( value1, value2 );
//第一种
     pair<string, int>p("tom", 36);
     cout << "姓名:" << p.first << "年龄:" << p.second;
     //第二种
     pair<string, int> p2 = make_pair("jerry", 95);
     cout << "姓名:" << p2.first << "年龄:" << p2.second;
8. set容器排序
set容器默认排承规则为从小到大
 利用仿函数。可以改变排序规则
内置排序指定规则
class myCompare 
 {
 public:
     //重载()
     bool operator()(int v1, int v2) const
     {
         return v1 > v2;
     }
 };
 void test49789()
 {
     //指定排序规则
    set<int, myCompare> s2;
     s2.insert(1235);
     s2.insert(255);
     s2.insert(343);
     s2.insert(678);
     //遍历
     for (set<int, myCompare>::iterator it = s2.begin(); it != s2.end(); it++) 
     {
         cout << "  " << *it;
     }
     cout << endl;
 }
自定义数据类型排序
class Person
 {
 public:
     string m_Name;
     int m_Age;
     int m_Height;
    Person(string name, int age, int height)
     {
         this->m_Age = age;
         this->m_Height = height;
         this->m_Name = name;
     }
};
 class ComparePersonHH
 {
 public:
     //重载()
     bool operator()(const Person &p1,const Person &p2)  const
     {
         //按照年龄降序
         return p1.m_Age > p2.m_Age;
     }
 };
 void test0145() 
 {
     //自定义数据类型都会指定排序规则
     set<Person, ComparePersonHH> s;
    //准备数据
     Person p1("hey", 25, 175);
     Person p2("kiring", 35, 195);
     Person p3("iron man", 45, 180);
     Person p4("spide man", 25, 168);
    s.insert(p1);
     s.insert(p2);
     s.insert(p3);
     s.insert(p4);
     for (set<Person, ComparePersonHH>::iterator it = s.begin(); it != s.end(); it++) 
     {
cout << " 姓名:" << (*it).m_Name << " 身高:" << (*it).m_Height << " 年龄:" << (*it).m_Age << endl;
}
}
二、map
1.基本概念
简介:
  map中所有元素都是pair
  pair中第一个元素为key(键值),起到索引作用,第二个元素为value (实值)
所有元素都会根据元素的键值自动排序
本质:
  map/multimap属于关联式容器。底层结构是用二叉树实现。
优点:
可以根据key值快速找到value值
map和multimap区别:
 map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
2.构造函数
函数原型:
构造;
 map<T1,T2> mp;               //map默认构造函数:
 map( const map &mp);      //拷贝构造函数
 赋值:
 map& operator=(onst map &mp);   //重载等号操作符
void printMap(map<int, int>& m) 
 {
     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
         cout << " key:" << (*it).first << " value = " << it->second << endl;
     }
     cout << endl;
 }
 void test430()
 {
     //创建map容器
     map<int, int> m;
     m.insert(pair<int, int>(1, 23));
     m.insert(pair<int, int>(3, 26));
     m.insert(pair<int, int>(4, 87));
     m.insert(pair<int, int>(5, 69));
printMap(m);
    //拷贝构造
     map<int, int> m2(m);
     printMap(m2);
    //赋值
     map<int, int>m3;
     m3 = m2;
     printMap(m3);
3. map大小和交换
函数原型:
 size();          //返回容器中元素的数目
 empty();      //判断容器是否为空
 swap(st);     //交换两个集合容器
4. map插入和删除
函数原型:
 insert(elem);   //在容器中插入元素。
[ ]不建议插入,用途可以利用key访问到value

 clear();            //清除所有元素
 erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的迭代器。

 erase(beg,end); //删除区间[beg,end]的所有元素,返回下一个元素的迭代器。
erase( key); //删除容器中值为key的元素。
5. map查找和统计
函数原型:
 find(key);       //查找key是否存在.若存在,返回该键的元素的迭代器;若不存在,返回map.end();

count(key); //统计key的元素个数
6.排序
map容器默认排序规则为按照key值进行从小到大排序
 利用仿函数,可以改变排序规则
class CompareM {
 public :
     bool operator()(int v1,int v2) const
     {
         return v1 > v2;
     }
 };
 void sortMap() 
 {
     map<int, int, CompareM>m;
    m.insert(make_pair(2, 100));
     m.insert(make_pair(4, 10));
     m.insert(make_pair(6, 1000));
     m.insert(make_pair(7, 10000));
    for (map<int, int , CompareM>::const_iterator it = m.begin(); it != m.end(); it++) {
         cout << " key:" << (*it).first << " value = " << it->second << endl;
     }
 }



















