STL 集合中的插入和刪除 C++


插入

透過 insert() 和 emplace() 操作可在 STL 集合中插入元素。

Insert():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()

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

更新於: 2019 年 7 月 30 日

900 次觀看

助力您的事業

完成課程以獲得認證

立即著手
廣告
© . All rights reserved.