📜  如何通过在C++中传递其值从Set中删除元素

📅  最后修改于: 2021-05-30 05:28:57             🧑  作者: Mango

给定一个Set,任务是从C++中从该Set中删除最后一个元素。
例子:

Input: set = [10 20 30 70 80 90 100 40 50 60],
              valueOfElementToBeDeleted = 100
Output: 10 20 30 40 50 60 70 80 90

Input: set = [1 2 3 4 5],
              valueOfElementToBeDeleted = 3
Output: 1 2 4 5

集是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。尽管可以删除并添加该元素的修改后的值,但是一旦将元素的值添加到集合中就无法对其进行修改。
方法:在此方法中,通过使用擦除函数并以最后一个元素的值作为参数来调用它,来删除最后一个元素。如果最后一个元素的值未知,则使用先前的方法。
句法:

size_type erase (const value_type& valueOfElementToBeDeleted);

下面是上述方法的实现:

C++
// C++ program to delete an element
// of a Set by passing its value
 
#include 
#include 
using namespace std;
 
// Function to print the set
void printSet(set myset)
{
 
    // Get the iterator
    set::iterator it;
 
    // printing all the elements of the set
    for (it = myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';
}
 
// Function to delete the element of set
void deleteByValue(set myset,
           int valueOfElementToBeDeleted)
{
 
    // printing all the elements of the set
    cout << "\nSet originally: ";
    printSet(myset);
 
    // Erase the element that is equal to
    // the value passed as the parameter
    myset.erase(valueOfElementToBeDeleted);
 
    // printing all the elements of the set
    cout << "Set after deleting "
         << valueOfElementToBeDeleted
         << ": ";
    printSet(myset);
}
 
// Driver code
int main()
{
    set myset;
 
    // Get the set
    for (int i = 1; i < 10; i++)
        myset.insert(i * 10);
 
    // Get the valueOfElementToBeDeleted
    int valueOfElementToBeDeleted = 50;
 
    // Delete an element from the Set
    deleteByValue(myset, valueOfElementToBeDeleted);
 
    return 0;
}


Java
// Java program to delete an element
// of a Set by passing its value
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to print the set
  static void printSet(TreeSet myset)
  {
 
    // printing all the elements of the set
    for(int it : myset)
    {
      System.out.print(it + " ");
    }
    System.out.println();
  }
 
  // Function to delete the element of set
  static void deleteByValue(TreeSet myset, 
                            int valueOfElementToBeDeleted)
  {
 
    // printing all the elements of the set
    System.out.print("\nSet originally: ");
    printSet(myset);
 
    // Erase the element that is equal to
    // the value passed as the parameter
    myset.remove(valueOfElementToBeDeleted);
 
    // printing all the elements of the set
    System.out.print("Set after deleting " +
                     valueOfElementToBeDeleted +  ": ");
    printSet(myset);
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    //Set myset = new HashSet();
    TreeSet myset = new TreeSet();
 
    // Get the set
    for (int i = 1; i < 10; i++)
    {
      myset.add(i * 10);
    }
 
    // Get the valueOfElementToBeDeleted
    int valueOfElementToBeDeleted = 50;
 
    // Delete an element from the Set
    deleteByValue(myset, valueOfElementToBeDeleted);
  }
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 program to delete an element of a
# set by passing of a set by passing its value
 
# function to print the set
def printset(myset):
     
    for i in myset:
        print(i, end = " ")
    print()
 
# function to delete the element of a set
def deleteByValue(myset, value):
     
    myset = sorted(myset)
     
    # printint original value
    print("Set orginally:", end = " ")
    printset(myset)
     
    # Erase the element that is equal to
    # the value passed as the parameter
    myset.remove(value)
     
    # printing all the elements of the set
    print("Set after deleting", value,
                        ":", end = " ")
    printset(myset)
 
# Driver code
myset = set()
 
for i in range(1, 10):
    myset.add(i * 10)
 
# Get the valueOfElementToBeDeleted
value = 50
 
# Delete an element from the Set
deleteByValue(myset, value);
 
# This code is contributed
# by Mohit kumar 29


C#
// C# program to delete an element
// of a Set by passing its value
using System;
using System.Collections.Generic;
public class GFG
{
 
  // Function to print the set
  static void printSet(HashSet myset)
  {
 
    // printing all the elements of the set
    foreach(int it in myset)
    {
      Console.Write(it + " ");
    }
    Console.WriteLine();
  }
 
  // Function to delete the element of set
  static void deleteByValue(HashSet myset, int valueOfElementToBeDeleted)
  {
 
    // printing all the elements of the set
    Console.Write("\nSet originally: ");
    printSet(myset);
 
    // Erase the element that is equal to
    // the value passed as the parameter
    myset.Remove(valueOfElementToBeDeleted);
 
    // printing all the elements of the set
    Console.Write("Set after deleting " + valueOfElementToBeDeleted +  ": ");
    printSet(myset);
  }
 
  // Driver code
  static public void Main ()
  {
    //Set myset = new HashSet();
    HashSet myset = new HashSet();
 
    // Get the set
    for (int i = 1; i < 10; i++)
    {
      myset.Add(i * 10);
    }
 
    // Get the valueOfElementToBeDeleted
    int valueOfElementToBeDeleted = 50;
 
    // Delete an element from the Set
    deleteByValue(myset, valueOfElementToBeDeleted);
  }
}
 
// This code is contributed by rag2127


输出:
Set originally:  10 20 30 40 50 60 70 80 90
Set after deleting 50:  10 20 30 40 60 70 80 90
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”