黑马程序员C++提高编程--STL容器之set与multiset

1.set基本概念

功能描述:

  • set容器,也叫关联式容器,内部自动排序,不允许重复元素
  • 所有元素在插入时自动被排序

本质:

  • set/multiset属于关联式容器,底层结构是用二叉树实现

set和multiset区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

2.set构造和赋值

功能描述: 创建set容器以及赋值

构造:

  • set st;// 默认构造函数
  • set(const set &st);//拷贝构造函数

赋值:

  • set& operator=(const set &st);//赋值操作符重载

示例代码:

#include 
#include 
using namespace std;

void printSet(set<int> &s)
{
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

// 构造和赋值
void test01()
{
    set<int> s1;

    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    printSet(s1);

    // 拷贝构造
    set<int> s2(s1);
    printSet(s2);

    // 赋值
    set<int> s3;
    s3 = s1;
    printSet(s3);

}

int main()
{
    test01();
    return 0;
}

总结:

  • set容器插入数据时用insert
  • set容器插入数据的数据会自动排序

3.set大小和交换

功能描述:

  • 统计set容器大小以及交换set容器

函数原型:

  • size();//返回容器中元素的数目
  • empty();//判断容器是否为空
  • swap(set &st);//交换两个set容器

示例代码:

#include 
#include 
using namespace std;

void printSet(set<int> &s)
{
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

// 大小
void test01()
{
    set<int> s1;

    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);

    if (s1.empty())
    {
        cout << "s1为空" << endl;
    }
    else
    {
        cout << "s1不为空" << endl;
        cout << "s1的大小为:" << s1.size() << endl;
    }
}

// 交换
void test02()
{
    set<int> s1;
    set<int> s2;

    cout << "交换前" << endl;

    s1.insert(100);
    s1.insert(200);
    printSet(s1);

    s2.insert(10);
    s2.insert(20);
    printSet(s2);

    cout << "交换后" << endl;
    s1.swap(s2);
    printSet(s1);
    printSet(s2);
}

int main()
{
    test01();
    test02();
    return 0;
}

总结:

  • 统计大小 — size
  • 判断是否为空 — empty
  • 交换容器 — swap

4.set插入和删除

功能描述:

  • set容器插入和删除元素

函数原型:

  • insert(elem);//在容器中插入元素
  • clear();//清空所有元素
  • erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg, end);//删除区间[beg, end)的所有元素,返回下一个元素的迭代器
  • erase(elem);//删除容器中值为elem的元素

示例代码:

#include 
#include 
using namespace std;

void printSet(set<int> &s)
{
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

// 插入和删除
void test01()
{
    set<int> s1;
    //插入
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);

    //删除
    s1.erase(s1.begin());
    printSet(s1);

    s1.erase(20);
    printSet(s1);


    // 清空
    // 方法1.s.erase(s.begin(), s.end());
    s1.clear(); // 方法2.s.clear();
    printSet(s1);

}

int main()
{
    test01();
    return 0;
}

总结:

  • 插入元素 — insert
  • 删除元素 — erase
  • 清空元素 — clear

5.set查找和统计

功能描述:

  • set容器查找和统计元素

函数原型:

  • find(key);//查找key是否存在,若存在,返回该元素的迭代器;若不存在,返回set.end()
  • count(key);//统计key的元素个数

示例代码:

#include 
#include 
using namespace std;

// 查找和统计
void test01()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    s1.insert(50);

    // 查找
    set<int>::iterator pos = s1.find(30);

    if (pos != s1.end())
    {
        cout << "找到元素:" << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    // 统计
    int num = s1.count(10);
    cout << "10出现的次数:" << num << endl;
}

int main()
{
    test01();
    return 0;
}

总结:

  • 查找元素 — find(返回的是迭代器)
  • 统计元素 — count(对于set,结果为0或1)

6.set和multiset区别

区别:

  • set不可以插入重复元素,multiset可以插入重复元素
  • set插入数据的同时会返回插入数据的位置表示插入是否成功
  • multiset不会检测数据,可以插入重复数据

示例代码:

#include 
#include 
using namespace std;

// set和multiset区别
void test01()
{
    set<int> s1;
    pair<set<int>::iterator, bool> ret = s1.insert(10);
    if (ret.second)
    {
        cout << "第一次插入成功" << endl;
    }
    else
    {
        cout << "第一次插入失败" << endl;
    }

    ret = s1.insert(10);
    if (ret.second)
    {
        cout << "第二次插入成功" << endl;
    }

    else
    {
        cout << "第二次插入失败" << endl;
    }

    // multiset
    multiset<int> ms;
    ms.insert(10);
    ms.insert(10);

    for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;


}

int main()
{
    test01();
    return 0;
}

总结:

  • set和multiset区别:如果允许插入重复数据就要用multiset容器,如果不允许重复数据就要用set容器

7.pair对组创建

功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair p(value1, value2);
  • pair p = make_pair(value1, value2);

示例代码:

#include 
#include 

using namespace std;

// 对组创建
void test01()
{
    pair<string, int> p(string("Tom"), 20);
    cout << "姓名:" << p.first << " 年龄:" << p.second << endl;

    pair<string, int> p2 = make_pair("Jerry", 18);
    cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

int main()
{
    test01();
    return 0;
}

总结: 两种方式都可以创建对组,记住一种即可

8.set容器排序

set容器默认排序规则为从小到大,掌握如何改变排序规则

主要技术点:

  • 利用仿函数,可以改变排序规则
示例一

set存放内置数据类型

#include 
#include 
using namespace std;

class MyCompare
{
public:
    bool operator()(int v1, int v2) const   // 我使用的编译器是gcc,要求它必须是 const 可调用的对象;老师使用的编译器是msvc,对const约束较宽松,因此讲义和视频里不加const也能运行)
    {
        return v1 > v2;
    }
};

void test01()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    s1.insert(50);

    // 默认从小到大
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    // 指定排序规则
    set<int, MyCompare> s2;
    s2.insert(10);
    s2.insert(30);
    s2.insert(20);
    s2.insert(40);
    s2.insert(50);
    for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
    test01();
    return 0;
}

总结:
利用仿函数可以指定set容器的排序规则

示例二

set存放自定义数据类型

#include 
#include 
#include 
using namespace std;

class Person
{
public:
    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
    string name;
    int age;
};
class PersonCompare
{
public:
    bool operator()(const Person& p1, const Person& p2) const
    {
        // 按照年龄进行排序,降序
        return p1.age > p2.age;
    }
};

void test01()
{
    set<Person, PersonCompare> s;
    Person p1("Tom", 10);
    Person p2("Jerry", 12);
    Person p3("Mike", 15);
    Person p4("Kate", 18);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person, PersonCompare>:: iterator it = s.begin(); it != s.end(); it++)
    {
        cout << "姓名:" << it->name << "  年龄:" << it->age << endl;
    }
    cout << endl;
}

int main()
{
    test01();
    return 0;
}

总结:
对于自定义数据类型,set必须指定排序规则才可以插入数据

你可能感兴趣的:(黑马程序员C++提高编程--STL容器之set与multiset)