【C++ STL】multisetのinsert()関数の使い方とサンプルコードを解説
C++ STLのmultiset(マルチセット)コンテナにおけるinsert()関数は、新しい要素をコンテナへ挿入するためのメンバ関数です。単に値を指定して挿入する形式と、挿入位置のヒントとなるイテレータを指定する形式があり、状況に応じて使い分けられます。multisetは重複した値を許可し、挿入された要素は常に自動的にソートされた状態で保持されるという特徴を持っています。
使用する主なメンバ関数
- ms.size() … マルチセットに現在格納されている要素数を返します。
- ms.insert(値) … 指定した値をマルチセットに挿入し、挿入された要素を指すイテレータを返します。
- ms.insert(イテレータ, 値) … 第1引数のイテレータを位置のヒントとして値を挿入します。ヒントが適切であれば、挿入処理が高速化される場合があります。
- ms.empty() … コンテナが空であるかどうかを判定します。
サンプルコード
次のプログラムは、メニュー形式でマルチセットのサイズ確認・要素の挿入・内容の表示を行う対話型のサンプルです。
#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<<"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<<"Elements of the Multiset: ";
for (it = ms.begin(); it != ms.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
実行結果
1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 1 Size of the Multiset: 0 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 2 Enter value to be inserted: 1 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 2 Enter value to be inserted: 2 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 3 Elements of the Multiset: 1 2 4 6 7 1.Size of the Multiset 2.Insert Element into the Multiset 3.Display Multiset 4.Exit Enter your Choice: 4 exit status 1
コードのポイント解説
- 初回の挿入:コンテナが空の場合は
ms.empty()がtrueを返すため、値だけを指定するms.insert(i)を呼び出しています。 - 2回目以降の挿入:直前の挿入で取得したイテレータ
it1を位置のヒントとして渡すms.insert(it1, i)を使用しています。 - 自動ソート:挿入した順序に関係なく、表示時には「1 2 4 6 7」と昇順に整列されています。これはmultisetが内部で常にソート済みの状態を維持するためです。
- 重複の扱い:同じ値を複数回挿入してもエラーにはならず、すべての要素が保持されます。これは重複を許さないsetとの大きな違いです。
-
C++ STLのacos()関数とは?使い方をサンプルコード付きで解説
C++ STLのacos()関数とはacos()関数は、ラジアン単位で与えられた値に対する逆余弦(アークコサイン)を返す関数で、C++のSTLに標準で組み込まれています。この関数は<cmath>ヘッダで定義されています。acos()関数の構文acos(var)構文から分かるように、acos()関数はfloat、double、long doubleのいずれかのデータ型を持つ引数varを1つ受け取ります。引数の値は-1から1の範囲内でなければなりません。範囲外の値を渡した場合はNaN(非数)が返される点に注意が必要です。戻り値は、-πからπの範囲におけるvarの逆余弦(ラジアン単位)で
-
C++ STLのasinh()関数とは?使い方とサンプルコードを解説
asinh()関数とはasinh()関数は、ラジアンで指定された角度に対する逆双曲線正弦(アークハイパボリックサイン)を返す関数です。C++ STLに標準で組み込まれており、<cmath>ヘッダーをインクルードすることで利用できます。構文asinh(var)上記の構文が示すように、asinh()関数はfloat型、double型、またはlong double型の引数varを1つ受け取ります。この引数には、負の値・正の値・0のいずれも指定可能です。関数はvarの逆双曲線正弦を返します。asinh()関数の基本的な使用例以下のプログラムは、C++でのasinh()関数の使用方法を示した