STLにセットを実装するC++プログラム
セットは、要素の値が要素を識別するため、各要素が一意である必要がある抽象データ型です。要素の値は、セットに追加されると変更できませんが、その要素の変更された値を削除して追加することはできます。
機能と説明:
Functions used here: st.size() = Returns the size of set. st.insert() = It is used to insert elements to the set. st.erase() = To delete the element from the set. st.find() = Returns an iterator to the search element in the set if found, else returns the iterator to end. st.begin() = Returns an iterator to the first element in the set. st.end() = Returns an iterator to the last element in the set.
サンプルコード
#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
set<int> st;
set<int>::iterator it;
int c, i;
while (1) {
cout<<"1.Size of the Set"<<endl;
cout<<"2.Insert Element into the Set"<<endl;
cout<<"3.Delete Element of the Set"<<endl;
cout<<"4.Find Element in a Set"<<endl;
cout<<"5.Display the set: "<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Size of the Set: ";
cout<<st.size()<<endl;
break;
case 2:
cout<<"Enter value to be inserted: ";
cin>>i;
st.insert(i);
break;
case 3:
cout<<"Enter the element to be deleted: ";
cin>>i;
st.erase(i);
break;
case 4:
cout<<"Enter the element to be found: ";
cin>>i;
it = st.find(i);
if (it != st.end())
cout<<"Element "<<*it<<" found in the set" <<endl;
else
cout<<"No Element Found"<<endl;
break;
case 5:
cout<<"Displaying Set by Iterator: ";
for (it = st.begin(); it != st.end(); it++) {
cout << (*it)<<" ";
}
cout<<endl;
break;
case 6:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} 出力
1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 1 Size of the Set: 0 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 2 Enter value to be inserted: 1 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 3 Enter the element to be deleted: 1 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 4 Enter the element to be found: 7 Element 7 found in the set 1.Size of the Set 2.Insert Element into the Set 3.Delete Element of the Set 4.Find Element in a Set 5.Display the set: 6.Exit Enter your Choice: 6 Exit code: 1
-
STLにSet_Intersectionを実装するC++プログラム
2つのセットの共通部分は、両方のセットに共通する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_intersection (set1, set1 + n, set2, s
-
STLにSet_Differenceを実装するC++プログラム
2つのセットの違いは、2番目のセットではなく、最初のセットに存在する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_difference (set1, set1 + n,