Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - REMOVE FROM LIST


How to remove an element from the List by its name/value?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the List.


It can be done with the remove() Function


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
    x.remove("Kriti");

    for (string data : x) {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :



  Mohan
  Salim

So, in the above code we have created a List and initialised to the variable x.


list<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

java_Collections

Next, we have used the remove() function that searches for the name Kriti and removes it from the List.


x.remove("Kriti");
java_Collections


And we get the below output,

Output :



  Mohan
  Salim

How to remove an element from the beginning of a List?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Mohan from the List.


It can be done with the pop_front() Method


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
        
    x.pop_front();
    for (string data : x)  
    {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :



  Kriti
  Salim

So, in the above code we have created a List and initialised to the variable x.


list<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

java_Collections

Next, we have used the pop_front() method that searches for the first element(And the first element is Mohan) and removes it from the List.


x.pop_front();
java_Collections

Output :



  Kriti
  Salim

How to remove an element from the end of a List?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Salim from the List.


It can be done with the pop_front() Method


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
        
    x.pop_back();
    for (string data : x)  
    {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :



  Mohan
  Kriti

So, in the above code we have created a List and initialised to the variable x.


list<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

java_Collections

Next, we have used the pop_front() method that searches for the first element(And the first element is Mohan) and removes it from the List.


x.pop_back();
java_Collections

Output :



  Mohan
  Kriti

How to remove all the elements from the List?


The clear() Method can be used to remove all the elements from the List.


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
        
    x.clear();
    for (string data : x)  
    {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :




So, in the above code we have created a List and initialised to the variable x.


list<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

java_Collections

Next, we have used the clear() method that removes all the elements from the List making the List empty.


x.clear();

And we get an empty List as output.