zoukankan      html  css  js  c++  java
  • 12.红黑树set

     1 #include <iostream>
     2 //红黑树(自动保证平衡,自动生成平衡查找树)
     3 #include <set>
     4 #include <cstring>
     5 #include <cstdlib>
     6 using namespace std;
     7 
     8 //红黑树增删查改
     9 void main1()
    10 {
    11     set<int> myset{ 1,2,10,3,5 };
    12     myset.insert(14);
    13     myset.insert(15);
    14     //重复的数据会被忽略
    15     myset.insert(15);
    16 
    17     //中序遍历
    18     for (auto i : myset)
    19     {
    20         cout << i << endl;
    21     }
    22 
    23     //正向迭代
    24     for (auto ib = myset.begin(), ie = myset.end(); ib != ie; ib++)
    25     {
    26         cout << *ib << endl;
    27     }
    28 
    29     //反向迭代
    30     for (auto rb = myset.rbegin(), re = myset.rend(); rb != re; rb++)
    31     {
    32         cout << *rb << endl;
    33     }
    34 
    35     auto it = myset.find(14);
    36     if (it != myset.end())
    37     {
    38         cout << "find" << *it << endl;
    39         //不能修改
    40         //*it = 9;
    41         //删除
    42         myset.erase(it);
    43         //或者
    44         //myset.erase(10);
    45     }
    46     else
    47     {
    48         cout << "not find" << *it << endl;
    49     }
    50 
    51     //元素个数
    52     myset.size();
    53     //清空
    54     myset.empty();
    55     cin.get();
    56 }
    57 
    58 struct strless
    59 {
    60     bool operator()(const char *str1, const char *str2)
    61     {
    62         return (strcmp(str1, str2) < 0);
    63     }
    64 };
    65 
    66 void main()
    67 {
    68     const char *p[] = { "calc","notepad","run","go" ,"123"};
    69     //设置比较函数 前边是类型,后边是比较的函数
    70     set<const char *, strless> myset(p, p + 5, strless());
    71     myset.insert("234");
    72 
    73     for (auto i : myset)
    74     {
    75         cout << i << endl;
    76     }
    77     cin.get();
    78 }
  • 相关阅读:
    moc处理cpp文件
    程序员!你还能年轻几岁?
    多媒体会议系统中的延迟
    把C++类成员函数集成到lua
    Q_PROPERTY使用
    python与c的集成
    注册C函数与类成员函数到lua
    摄像头(WebCam)在Linux操作系统中的驱动方法
    网络营销实战必读之书推荐:《网络营销实战密码》
    这样写的博客才有更多的人愿意看
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8627416.html
Copyright © 2011-2022 走看看