STLセットC++での挿入と削除
挿入
STLセットへの挿入は、insert()およびemplace()操作によって実行できます。
Insert() :Insert()は、要素をセットに挿入するために使用されます。挿入操作はオブジェクトへの参照を取ります。
使用される関数のリスト:
- st.size()=セットのサイズを返します。
- st.insert()=要素をセットに挿入するために使用されます。
サンプルコード
#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.Display the set: "<<endl;
cout<<"4.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<<"Displaying Set by Iterator: ";
for (it = st.begin(); it != st.end(); it++) {
cout << (*it)<<" ";
}
cout<<endl;
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} 出力
1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 1 Size of the Set: 0 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 8 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 10 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 3 Displaying Set by Iterator: 4 6 8 10 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 4 Exit code: 1
Emplace()
配置操作は、セットに要素をインプレースで挿入するためにも使用されます。オブジェクトの不要なコピーを回避し、挿入操作よりも効率的に挿入を行います。
使用されている関数のリスト:
- st.size()=セットのサイズを返します。
- st.emplace()=要素をセットに挿入するために使用されます。
サンプルコード
#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.Display the set: "<<endl;
cout<<"4.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.emplace(i);
break;
case 3:
cout<<"Displaying Set by Iterator: ";
for (it = st.begin(); it != st.end(); it++) {
cout << (*it)<<" ";
}
cout<<endl;
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} 出力
1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 1 Size of the Set: 0 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 6 Wrong Choice 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 2 Enter value to be inserted: 8 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 3 Displaying Set by Iterator: 4 6 7 8 1.Size of the Set 2.Insert Element into the Set 3.Display the set: 4.Exit Enter your Choice: 4 Exit code: 1
削除
Erase()関数を使用すると、引数、位置、値、または数値の範囲のいずれかを指定することで、セットから要素を削除できます。
ここで使用される関数のリスト:
- st.size()=セットのサイズを返します。
- st.insert()=要素をセットに挿入するために使用されます。
- st.erase()=セットから要素を削除するには
サンプルコード
#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 from the Set"<<endl;
cout<<"4.Display the set: "<<endl;
cout<<"5.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<<"Displaying Set by Iterator: ";
for (it = st.begin(); it != st.end(); it++) {
cout << (*it)<<" ";
}
cout<<endl;
break;
case 5:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} 出力
1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.Exit Enter your Choice: 1 Size of the Set: 0 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.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 from the Set 4.Display the set: 5.Exit Enter your Choice: 2 Enter value to be inserted: 2 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.Exit Enter your Choice: 2 Enter value to be inserted: 3 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.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 from the Set 4.Display the set: 5.Exit Enter your Choice: 4 Displaying Set by Iterator: 1 2 3 4 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.Exit Enter your Choice: 3 Enter the element to be deleted: 2 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.Exit Enter your Choice: 4 Displaying Set by Iterator: 1 3 4 1.Size of the Set 2.Insert Element into the Set 3.Delete Element from the Set 4.Display the set: 5.Exit Enter your Choice: 5 Exit code: 1
-
C ++ STLでlower_bound()関数を設定します
C ++でsetlower_bound()関数を設定すると、STLは、パラメーターで渡されたkに相当するコンテナー内の要素を指すイテレーターを返します。セットコンテナにkが存在しない場合、関数はkより少し大きい直前の要素を指すイテレータを返します。 アルゴリズム Begin Initialize an empty set container s. Initializing a set container as inetrator. Insert some elements in s set container. &n
-
C ++ STLでfind()関数を設定します
C ++STLのsetfind()関数は、setコンテナで検索される要素にイテレータを返します。要素が見つからない場合、イテレータはセット内の最後の要素の直後の位置を指します。 アルゴリズム Begin Define function printS() to print elements of set container. initialize an empty set container s. Insert some elements in s set container. Call function to pri