Stl-set (collection) deletion element _stl-set

Source: Internet
Author: User

Set Overview


Unlike vectors and lists, set and map are associative containers. The set interior is based on the red and black tree. Insert and delete operations are more efficient because you only need to modify the relevant pointers without moving the data.
After the data deletion operation, the iterator will not be invalidated. When you delete a set's data, the actual action is to delete a node in the red-black tree, and then make related adjustments to the associated pointer. The iterator pointing to the other element still points to the original location and does not change, so the other iterator does not fail after you delete one node. The list and map are the same. However, to remove an element from the vector, other iterators in the vector will fail because the vector is based on an array, and after the deletion of an element, the subsequent elements move forward, so the iterator that points to the following element is invalidated.
Say a little bit more about the implementation of the iterator. An iterator is an object in which the vector iterator encapsulates an array subscript, and a list, map, set iterator is a pointer that encapsulates an element node.
And one more thing, from the mathematical level, a set of sets, like a bag with a lot of balls inside. But the red-black tree is a special two-fork search tree in which the elements in a set have a specific position in the red-black tree, and are not removable. So, 1 is the search operation efficiency will be very high O (log n), 2 is the value of the element in set can not be changed.


Data manipulation of Set

:: Begin ()/     /iterator
:: End ()//iterator
:: Clear ()   //Delete all elements in the set container:
: Empty ()//To determine if the set container is empty
: max_ Size ()//returns the maximum number of elements that the set container may contain
:: Size ()//Returns the number of elements in the current set container
:: Rbegin//Inverse iterator
:: rend ()//Inverse iterator

Small problem
Set is based on a red-black tree, then where does the set's iterator begin () and end () point to?
A test program:

#include <iostream>
#include <set>
using namespace std;
int main ()
{
    set<int> myset;
    Myset.insert (4);
    Myset.insert (7);
    Myset.insert (2);
    Myset.insert (0);
    Myset.insert (4);
    Set<int>::iterator it;
    for (it = Myset.begin (); it!= myset.end (); it++)
    {
        cout<< *it;   Output result: 0247
    }
}

The red-black tree is the first two-fork search tree, so the begin () iterator points to the leftmost node of the red-black tree, and the end () iterator points to the rightmost node of the red-black tree. In addition, this applet shows that repeated inserts are not valid.


Order deletion

Delete odd or even numbers between 1-100:

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
int main ()
{
    set<int>s;
    for (int i=100; i; i--)
        S.insert (i);
    Set<int>::iterator it;
    For (It=s.begin (); It!=s.end (); it++)
    {
        cout<<*it<< "";
    }
    cout<< "\n*********" <<endl;
    For (It=s.begin (); It!=s.end (); it++)
    {
        if (*it&1)
            s.erase (it);
    }
    cout<< "\n*********" <<endl;
    For (It=s.begin (); It!=s.end (); it++)
    {
        cout<<*it<< "";
    }
    return 0;
}
This action will get the correct result, but the method is correct.

If you want to remove a multiple of 3, an error occurs.

How to write it correctly.

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
int main ()
{
    set<int>s;
    for (int i=100; i; i--)
        S.insert (i);
    Set<int>::iterator it;
    For (It=s.begin (); It!=s.end (); it++)
    {
        cout<<*it<< "";
    }
    cout<< "\n*********" <<endl;
    int i=0;
    For (It=s.begin (); It!=s.end (); it++)
    {
        i++;
        if ((*it)%3==0)
            s.erase (it++);
        else
            it++;
    }
    cout<< "\n*********" <<endl;
    For (It=s.begin (); It!=s.end (); it++)
    {
        cout<<*it<< "";
    }
    return 0;
}

Reason: The set (set) deletes elements in stl/c++__, and the set erase does not return an iterator, which requires attention.

Reference Blog: http://blog.csdn.net/doctor_feng/article/details/11836137

Respect original, reprint please indicate source: http://blog.csdn.net/hurmishine


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.