ইনসারশন
STL set-এ ইনসারশন insert() এবং emplace() অপারেশন দিয়ে করা যায়।
Insert()
Insert() সেটে এলিমেন্ট ইনসার্ট করতে ব্যবহৃত হয়। ইনসার্ট অপারেশন একটি অবজেক্টের রেফারেন্স নেয়।
ব্যবহৃত ফাংশন:
st.size()= সেটের সাইজ রিটার্ন করে।st.insert()= সেটে এলিমেন্ট ইনসার্ট করতে ব্যবহৃত হয়।
উদাহরণ কোড (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: " << 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 ... (ইন্টারঅ্যাক্টিভ আউটপুট) Displaying Set by Iterator: 4 6 8 10
Emplace()
Emplace অপারেশনও সেটে এলিমেন্ট ইনসার্ট করতে ব্যবহৃত হয় ইন-প্লেসে। এটি অবজেক্টের অপ্রয়োজনীয় কপি এড়িয়ে দিয়ে ইনসার্ট অপারেশনের চেয়ে বেশি দক্ষতার সাথে ইনসারশন করে।
ব্যবহৃত ফাংশন:
st.size()= সেটের সাইজ রিটার্ন করে।st.emplace()= সেটে এলিমেন্ট ইনসার্ট করতে ব্যবহৃত হয়।
উদাহরণ কোড (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: " << 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;
}আউটপুট
(ইন্টারঅ্যাক্টিভ আউটপুট, Emplace ব্যবহার করে এলিমেন্ট ইনসার্ট এবং ডিসপ্লে)
ডিলিশন
erase() ফাংশন ব্যবহার করে সেট থেকে এলিমেন্ট ডিলিট করা যায়, তার আর্গুমেন্ট হিসেবে পজিশন, ভ্যালু বা একটি রেঞ্জ দিয়ে।
ব্যবহৃত ফাংশন:
st.size()= সেটের সাইজ রিটার্ন করে।st.insert()= সেটে এলিমেন্ট ইনসার্ট করতে ব্যবহৃত হয়।st.erase()= সেট থেকে এলিমেন্ট ডিলিট করতে ব্যবহৃত হয়।
উদাহরণ কোড (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: " << 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 ... (ইন্টারঅ্যাক্টিভ আউটপুট) Displaying Set by Iterator: 1 3 4