zoukankan      html  css  js  c++  java
  • c++ set multiset

    #include <iostream>
    #include <set>

    using namespace std;

    template<typename Container>
    void PrintContents(const Container& con);

    template<typename Container>
    void PrintContents(const Container& con)
    {
      Container::const_iterator iter = con.begin();
      while (iter != con.end())
      {
        cout << *iter << " ";
        ++iter;
      }
      cout << endl;
    }

    int main()
    {
      set<int> set1;
      multiset<int> multiset1;

      set1.insert(10);
      set1.insert(20);
      set1.insert(30);
      set1.insert(20);

      cout << "size:" << set1.size() << endl;
      set<int>::const_iterator set_iter1 = set1.begin();
      while (set_iter1 != set1.end())
      {
        cout << *set_iter1 << " ";
        set_iter1++;
      }
      cout << endl;
      
      multiset1.insert(set1.begin(),set1.end());
      multiset1.insert(50);
      multiset1.insert(50);

      cout << "--------------------------------------------" << endl;
      cout << "multiset size:" << multiset1.size() << " " << multiset1.count(50) << endl;

      multiset<int>::const_iterator multiset_iter1 = multiset1.begin();
      while (multiset_iter1 != multiset1.end())
      {
        cout << *multiset_iter1 << " ";
        multiset_iter1++;
      }
      cout << endl;
      cout << "------------------------------------------- - " << endl;
      
      PrintContents(multiset1);

      cout << "------------------------------------------- - " << endl;
      set<int>::iterator iter_f = set1.find(20);
      if (iter_f != set1.end())
      {
        cout << "find: "<<*iter_f << endl;
      }
      else
      {
        cout << "not find:" << endl;
      }

      multiset1.erase(50);
      PrintContents(multiset1);

      system("pause");
      return 0;
    }

    =======================================================

    size:3
    10 20 30
    --------------------------------------------
    multiset size:5 2
    10 20 30 50 50
    ------------------------------------------- -
    10 20 30 50 50
    ------------------------------------------- -
    find: 20
    10 20 30
    请按任意键继续. . .

  • 相关阅读:
    事件基础
    DOM
    GoWeb编程之多路复用
    GoWeb编程之HelloWorld
    Linux libtins 库安装教程
    模式串匹配KMP详解
    树的重心
    Light OJ 1064
    Light OJ 1060
    1057
  • 原文地址:https://www.cnblogs.com/herd/p/10997706.html
Copyright © 2011-2022 走看看