Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

STL에서 다중 집합을 구현하는 C++ 프로그램

<시간/>

다중 집합은 여러 요소가 동일한 값을 가질 수 있는 연관 컨테이너 유형입니다.

기능 및 설명:

Functions are used here:
   ms.size() = Returns the size of multiset.
   ms.insert) = It is used to insert elements to the multiset.
   ms.erase() = Removes the value from the multiset.
   ms.find() = Returns an iterator to the search element in the multiset if found,
   else returns the iterator to end.
   ms.count() = Returns the number of matches element in the multiset.
   ms.begin() = Returns an iterator to the first element in the multiset.
   ms.end() = Returns an iterator to the last element in the multiset

예시 코드

#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.Delete Element from the Multiset"<<endl;
      cout<<"4.Find Element in a Multiset"<<endl;
      cout<<"5.Count Elements with a specific key"<<endl;
      cout<<"6.Display Multiset"<<endl;
      cout<<"7.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of the Multiset: "<<ms.size()<<endl;
         break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            if (ms.empty())
               it1 = ms.insert(i);
            else
               it1 = ms.insert(it1, i);
         break;
         case 3:
            cout<<"Enter value to be deleted: ";
            cin>>i;
            ms.erase(i);
         break;
         case 4:
            cout<<"Enter element to find ";
            cin>>i;
            it = ms.find(i);
            if (it != ms.end())
               cout<<"Element found"<<endl;
            else
               cout<<"Element not found"<<endl;
         break;
         case 5:
            cout<<"Enter element to be counted: ";
            cin>>i;
            cout<<i<<" appears "<<ms.count(i)<<" times."<<endl;
         break;
         case 6:
            cout<<"Elements of the Multiset: ";
            for (it = ms.begin(); it != ms.end(); it++)
               cout<<*it<<" ";
            cout<<endl;
         break;
         case 7:
            exit(1);
         break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

출력

1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 1
Size of the Multiset: 0
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 3
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 6
Elements of the Multiset: 1 2 3 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 3
Enter value to be deleted: 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 4
Enter element to find 1
Element found
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 5
Enter element to be counted: 2
2 appears 1 times.
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 7
Exit code: 1