zoukankan      html  css  js  c++  java
  • cb23a_c++_标准模板库STL_set_multiset_关联容器

    cb23a_c++_标准模板库STL_set_multiset_关联容器

    set(集)数据不能重复、multiset(多集)可以重复。操作数据速度快,数据自动排序。
    红黑树(数据结构)红黑树-二叉树
    基本操作
    insert: 插入时比vector稍慢
    count和find
    erase:
    注意:不能通过find进行修改(因为它会自动排序,位置就不确定),顺序容器vector可以修改。
    set只能查找,不能修改。vector可以。

    set可以先删除数据,再插入数据。
    welcome to disscuss
    txwtech@163.com

    vs2017 error C2760: 语法错误: 意外的令牌“标识符”,预期的令牌为“;”
    打开项目工程----> 属性 ---> c/c++ --> 语言 --> 符合模式 修改成否即可
    */

     1 /*cb23a_c++_标准模板库STL_set_multiset_关联容器
     2 
     3 set(集)数据不能重复、multiset(多集)可以重复。操作数据速度快,数据自动排序。
     4 红黑树(数据结构)红黑树-二叉树
     5 基本操作
     6 insert: 插入时比vector稍慢
     7 count和find
     8 erase: 
     9 注意:不能通过find进行修改(因为它会自动排序,位置就不确定),顺序容器vector可以修改。
    10 set只能查找,不能修改。vector可以。
    11 welcome to disscuss
    12 txwtech@163.com
    13 
    14 vs2017 error C2760: 语法错误: 意外的令牌“标识符”,预期的令牌为“;”
    15 打开项目工程----> 属性 ---> c/c++ --> 语言 --> 符合模式 修改成否即可
    16 */
    17 #include <iostream>
    18 #include <set>
    19 #include <string>
    20 
    21 using namespace std;
    22 template<typename Container> //使用模板函数,任何容器都可以显示
    23 void PrintContent(const Container &c);
    24 
    25 int main()
    26 {
    27     set<int> a;//是一个泛型
    28     multiset<int> ma;
    29     a.insert(60); //插入数据后,自动排序
    30     a.insert(-1);
    31     a.insert(3000);
    32     a.insert(60);//重复数据自动过滤掉
    33     cout << "显示set里面的数据: " << endl;
    34     //set<int>::const_iterator i = a.begin();
    35     /*while (i != a.end())
    36     {
    37         cout << *i << endl;
    38         ++i;
    39     }*/
    40     PrintContent(a);
    41     ma.insert(a.begin(), a.end());//a的数据全部插入到ma里面,multiset可以重复
    42     ma.insert(3000);
    43     cout << "multiset里面有 " << ma.count(3000) << "个3000" << endl;;//统计ma里面3000的个数
    44     
    45     cout << "显示ma里面的数据" << endl;
    46 
    47     //multiset<int>::const_iterator i2 = ma.begin();
    48     /*while (i2 != ma.end())
    49     {
    50         cout << *i2 << endl;
    51         ++i2;
    52     }*/
    53     PrintContent(ma);
    54     return 0;
    55 }
    56 template<typename Container>
    57 void PrintContent(const Container & c)
    58 {
    59     Container::const_iterator i = c.begin();
    60     while (i != c.end())
    61     {
    62         cout << *i << endl;
    63         ++i;
    64     }
    65 }
     1 /*cb23b
     2 SETINT::iterator i_found = a.find(-1);//set只能查找,不能修改。vector可以。因为set会自动排序。
     3 cout << "如果要修改数据,先删除数据,再插入数据" << endl;
     4 */
     5 #include <iostream>
     6 #include <set>
     7 
     8 using namespace std;
     9 typedef set<int> SETINT;//定义一个别名
    10 
    11 int main()
    12 {
    13     //set<int> a;
    14     SETINT a;////定义一个别名
    15     a.insert(43);
    16     a.insert(78);
    17     a.insert(-1);
    18     a.insert(124);
    19 
    20     SETINT::const_iterator i;
    21     for (i = a.begin(); i != a.end(); ++i)
    22         cout << *i << endl;
    23     a.find(-1);//返回的是迭代器
    24     SETINT::iterator i_found = a.find(-1);//set只能查找,不能修改。vector可以。因为set会自动排序。
    25     if (i_found != a.end())
    26         cout << "找到了: " <<*i_found<< endl;
    27     else
    28     {
    29         cout << "没有找到" << endl;//指针指向未知。显示*i_found会报错。
    30     }
    31 
    32     cout << "如果要修改数据,先删除数据,再插入数据" << endl;
    33     return 0;
    34 }
     1 /*
     2 
     3 */
     4 #include <iostream>
     5 #include <set>
     6 
     7 using namespace std;
     8 
     9 typedef multiset<int> MSETINT;
    10 
    11 int main()
    12 {
    13     MSETINT a;
    14     a.insert(43);
    15     a.insert(78);
    16     a.insert(78);
    17     a.insert(-1);
    18     a.insert(124);
    19     MSETINT::const_iterator i;
    20     cout << "multiset里有:" << a.size() << "个数据" << endl;
    21     cout << "显示每个数据:" << endl;
    22     for (i = a.begin(); i != a.end(); ++i)
    23     {
    24         cout << *i << endl;
    25     }
    26     cout << "要删除的数据是:" << endl;
    27     int nNumberToErase = 0;
    28     cin >> nNumberToErase;
    29     a.erase(nNumberToErase); //删除78时,两个78都删除
    30     cout << "multiset里现有:" << a.size() << "个数据" << endl;
    31     cout << "删除后每个数据:" << endl;
    32     for (i = a.begin(); i != a.end(); ++i)
    33     {
    34         cout << *i << endl;
    35     }
    36 
    37     a.clear();//删除全部数据、
    38     return 0;
    39 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    Oracle存储过程小记DUAL
    线程私有数据(TSD)
    Unix 五种基本I/O模型的区别
    Redis系列(0)应用场景
    linux ubuntu引导 win7
    Redis系列(一)启动流程分析
    c++ 内存管理小结
    设计模式Facade模式应用场景
    学会理财不做穷人
    jquery 注册验证例子
  • 原文地址:https://www.cnblogs.com/txwtech/p/12326698.html
Copyright © 2011-2022 走看看