【C++】STLのマルチセット(multiset)を実装・操作するサンプルプログラム
マルチセット(multiset)とは
マルチセットは、C++のSTL(標準テンプレートライブラリ)に用意されている連想コンテナの一種です。通常のstd::setと異なり、同じ値を持つ複数の要素を同時に格納できる点が最大の特徴です。また、要素は挿入時に自動的にソートされるため、常に整列された状態で管理されます。
使用する主なメンバ関数
| 関数 | 説明 |
|---|---|
| ms.size() | マルチセットに格納されている要素数を返します。 |
| ms.insert() | マルチセットへ新しい要素を挿入します。 |
| ms.erase() | 指定した値をマルチセットから削除します。 |
| ms.find() | 検索対象の要素が見つかった場合はその要素を指すイテレータを返し、見つからなかった場合はend()と同じイテレータを返します。 |
| ms.count() | マルチセット内でキーが一致する要素の個数を返します。 |
| ms.begin() | マルチセットの先頭要素を指すイテレータを返します。 |
| ms.end() | 末尾要素の次の位置を指すイテレータを返します。 |
サンプルコード
次のプログラムは、マルチセットに対して「サイズ取得」「要素の挿入」「要素の削除」「要素の検索」「特定キーのカウント」「内容の表示」といった操作を、メニュー形式で対話的に実行できるC++コードです。
#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;
}プログラムのポイント
- 重複の許可:multisetは同一の値を複数格納できます。count()を使うことで、同じキーがいくつ存在するかを確認できます。
- 挿入位置のヒント:選択肢2では、コンテナが空の場合は通常のinsert(i)を、空でない場合は直前の挿入位置を示すイテレータit1をヒントとしてinsert(it1, i)を呼び出しており、これにより挿入処理を効率化しています。
- 削除時の注意:erase(値)を呼び出すと、その値に一致するすべての要素が削除されます。1件だけ削除したい場合はイテレータを渡すerase(it)を使用します。
- 終了処理:選択肢7では<cstdlib>ヘッダで提供されるexit(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: 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
-
C++のSTLでset_intersectionを実装し、2つの集合の積集合を求める方法
2つの集合の積集合(インターセクション)とは、両方の集合に共通して含まれる要素だけを集めたものです。set_intersection関数によってコピーされる要素は、必ず最初の集合から取り出され、元の順序がそのまま維持されます。また、この関数を正しく動作させるためには、処理前に両方の集合がそれぞれソート済みである必要があります。 集合に対する代表的な操作には、以下のようなものがあります。 和集合(ユニオン) 積集合(インターセクション) 対称差(排他的論理和・XOR) 差集合(減算) アルゴリズム Begin 結果を格納するvector型変数vとイテレータstを宣言する。 st =
-
【C++】STLのset_differenceを使って2つの集合の差分を求める方法
2つの集合の「差(差集合)」とは、1つ目の集合には存在するが、2つ目の集合には存在しない要素だけから構成される集合のことです。set_difference関数によってコピーされる要素は、必ず1つ目の集合から取り出され、元の順序が保たれます。また、この関数を正しく動作させるためには、両方の集合があらかじめソート(整列)されている必要があります。代表的な集合演算には以下のようなものがあります。和集合(Union)積集合(Intersection)対称差(Symmetric Difference / 排他的論理和)差集合(Difference / 減算)アルゴリズムBegin 集合用のvec