Delete last 3 elements in list c++

c++ list delete element at index
c++ delete list of pointers
remove last element from list c#
remove element from array - c++
remove last element from list python
delete last element from array c++
remove element from vector c++
remove last element from list c++

I have this c++ code which works fine in deleting the last 3 elements of a list, but i was wondering if it's the correct way to do such a thing since i am worried of deleting elements with iterators issues. The code basically takes a list of 6 elements "Groups", divides it into 2 smaller lists "Group1" and "Group2", and then compares a different List "GroupToCompare" to "Group2" and if they're equal it removes the last 3 elements of "Groups".

#include "pch.h"
#include <iostream>
#include <iostream>
#include <string>
#include <list>
using namespace std;

int main()
{
    std::list <string> Groups = {};
    Groups = { "Spike", "Jet", "Faye", "Edward", "Vincent", "Elektra" };
    std::list<string> Group1 = {};
    std::list<string> Group2 = {};
    std::list<string> GroupToCompare = {};
    GroupToCompare = { "Edward", "Vincent", "Elektra" };

    size_t half1 = Groups.size() / 2;
    std::list<std::string>::iterator ig = Groups.begin();
    advance(ig, half1);
    Group1.insert(Group1.end(), Groups.begin(), ig);
    Group2.insert(Group2.end(), ig, Groups.end());

    std::list<std::string>::iterator removeIt = Groups.begin();
    advance(removeIt, half1);

    cout << "List Elements 1: " << endl;
    std::list<string>::iterator itrofList = Group1.begin();
    string firstvar;
    for (itrofList = Group1.begin(); itrofList != Group1.end(); ++itrofList) {
        firstvar = *itrofList;
        cout << "Item: " << firstvar << endl;
    }

    cout << "List Elements 2: " << endl;
    std::list<string>::iterator itrofList1 = Group2.begin();
    string firstvar1;
    for (itrofList1 = Group2.begin(); itrofList1 != Group2.end(); ++itrofList1) {
        firstvar1 = *itrofList1;

        cout << "Item: " << firstvar1 << endl;
    }

    if (Group2 == GroupToCompare) {
        removeIt = Groups.erase(removeIt);
        removeIt = Groups.erase(removeIt);
        removeIt = Groups.erase(removeIt);
    }

    cout << "List Elements of Groups after removing the last 3 elements: " << endl;
    std::list<string>::iterator itrofList2 = Groups.begin();
    string firstvar2;
    for (itrofList2 = Groups.begin(); itrofList2 != Groups.end(); ++itrofList2) {
        firstvar2 = *itrofList2;

        cout << "Item: " << firstvar2 << endl;
    }


}

is there a more correct way in case i wanna delete more elements in the end of the list and avoid deleting issues with iterators? Thanks in advance!

Even though calling std::list::erase() multiple times with the returned position works, calling the correct overload would help with readability and possibly performance:

iterator erase( iterator first, iterator last );

Example:

std::list<int> l{1, 2, 3, 4, 5};
l.erase(std::prev(l.end(), 3), l.end());
// l is {1, 2}

Beware: as-is, this code exhibit undefined behavior if l is not at least 3 in size.

How to delete last element from a List in C++ STL, Given a List, the task is to remove the last element from this List in C++. 100 40 50 60] Output: 10 20 30 40 50 60 70 80 90 Input: list = [1 2 3 4 5] Output: 1 2 3 4. The original list is : 10 15 20 25 30 35 The list after deleting first element using pop_front () : 15 20 25 30 35 The list after deleting last element using pop_back () : 15 20 25 30. Using remove () and remove_if () : remove () : This function deletes all the occurrences of the value passed in its arguments.

Is there a more correct way to do it?

Not sure what that means, but im assuming you would be interested in learning std::list has a public member function pop_back which deletes the last element in a list. Using this seems ideal for your case.

Note: As stated in this refference

If the container is not empty, the function never throws exceptions (no-throw guarantee). Otherwise, it causes undefined behavior.

So make sure the list is never empty when you call this function.

Delete elements in C++ STL list, How to delete last element from a map in C++ · new vs malloc() and free() vs delete in C++ · How to delete an element from the Set by passing its value in C++​  If the list contains only one element, then firstNode points to NULL and the only node is deleted. If there are more than two items in your list, your have to iterate to the last second element, set its next value to NULL, and delete the last node with the help of a temp pointer.

I would just...

std::list <string> Groups;

...

for (int i = 0; i < 3; ++i)
    Groups.pop_back();

Remove last element from an array in C, In this post, we will see how to remove the last element from an array in C#. 3. Convert to List<T>. The idea is to last convert the array into a List<T> , then use  If you want to remove the specific position element in a list, like the 2th, 3th and 7th. you can't use. del my_list[2] del my_list[3] del my_list[7] Since after you delete the second element, the third element you delete actually is the fourth element in the original list.

How to remove last element of a List in Java, In this quick article, we'll see how to remove last element of a List in Java. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. import java.util.ArrayList; Please use our online compiler to post code in comments using C, C++, Java, Python,  Some answers below do remove "Last n", so read answers carefully to see if they do what you want. – ToolmakerSteve Apr 27 '18 at 17:49 Don't go for the Queue based implementation if you want to sort your data – Martin Oct 10 '18 at 16:02

Python List remove(), Example 3: Deleting element that doesn't exist. # animals list animals = ['cat', 'dog'​, 'rabbit', 'guinea pig'] # Deleting 'fish' element animals.remove('fish')  Unlike member function list::erase, which erases elements by their position (using an iterator), this function ( list::remove) removes elements by their value. A similar function, list::remove_if, exists, which allows for a condition other than an equality comparison to determine whether an element is removed. Value of the elements to be removed.

Deleting from a Linked List, The last node links to nothing (i.e., there are no nodes after it). Also, there is a of the list. What do these links correspond to in C? There are a few steps to deleting a specific element from the list: Find the Let's consider at least 3 cases:​. Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted. In Java, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null. /* Inserts a new Node at front of the list.

Comments
  • Please, read minimal reproducible example.
  • Why don't you use pop_back (or pop_front)
  • @hellow seems like a good idea. (Y)