在 STL 中实现 Multiset 的 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#include #include #include using namespace std; int main() { multiset ms; multiset ::iterator it, it1; int c, i; while (1) { cout<<"1.Size of the Multiset"< >c; switch(c) { case 1: cout<<"多重集的大小: "< >i; if (ms.empty()) it1 = ms.insert(i); else it1 = ms.insert(it1, i); break; case 3: cout<<"输入要删除的值: "; cin>>i; ms.erase(i); break; case 4: cout<<"输入要查找的元素 "; cin>>i; it = ms.find(i); if (it != ms.end()) cout<<"Element found"< >i; cout<输出结果 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 输入您的选择: 1 多重集的大小: 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 输入您的选择: 2 输入要插入的值: 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 输入您的选择: 2 输入要插入的值: 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 输入您的选择: 2 输入要插入的值: 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 输入您的选择: 2 输入要插入的值: 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 输入您的选择: 6 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 输入您的选择: 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 输入您的选择: 4 输入要查找的元素 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 输入您的选择: 5 输入要计算的元素: 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 输入您的选择: 7 Exit code: 1