C ++ STL中的多集insert()函数
insert()C++STL中的多重集函数,可将元素从一个多重集到另一个多重集的位置从一个位置插入到另一个位置。
使用的函数列表:
ms.size()=返回多集的大小。
ms.insert()=用于将元素插入多集。
范例程式码
#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
   multiset<int> ms;
   multiset<int>::iterator it, it1;
   int c, i;
   while (1) {
      cout<<"1.Size of the Multiset"<<endl;
      cout<<"2.Insert Element into the Multiset"<<endl;
      cout<<"3.Display Multiset"<<endl;
      cout<<"4.Exit"<<endl;
      cout<<"输入您的选择: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"多重集的大小: "<<ms.size()<<endl;
         break;
         case 2:
            cout<<"输入要插入的值: ";
            cin>>i;
            if (ms.empty())
               it1 = ms.insert(i);
            else
               it1 = ms.insert(it1, i);
         break;
         case 3:
            cout<<"多重集的元素: ";
            for (it = ms.begin(); it != ms.end(); it++)
               cout<<*it<<" ";
            cout<<endl;
         break;
         case 4:
            exit(1);
         break;
         default:
            cout<<"选择错误"<<endl;
      }
   }
return 0;
}输出结果
1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 1 多重集的大小: 0 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 2 输入要插入的值: 1 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 2 输入要插入的值: 2 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 2 输入要插入的值: 4 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 2 输入要插入的值: 6 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 2 输入要插入的值: 7 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 3 多重集的元素: 1 2 4 6 7 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit 输入您的选择: 4 exit status 1