std::reverse_iterator::base()

#include 
#include 
#include 
#include 
#include 

int main()
{
    {   
        std::cout << "test erase item from std::vector using reverse_iterator" << std::endl; 
        std::vector v;
        for(int i = 0 ; i < 5 ; i++)
        {   
            v.push_back(i+1);
        }   
    
        std::vector::reverse_iterator ri; 
        for( ri = v.rbegin() ; ri != v.rend(); )
        {   
            if(*ri == 2 || *ri == 4)
            {   
                ri = std::vector::reverse_iterator( ++ v.erase( --ri.base() ) );   
            }   
            else
            {   
                ri++;    
            }   
        }   
        std::vector::iterator it; 
        for(it = v.begin() ; it != v.end() ; it++)
        {   
            std::cout << *it << std::endl;
        }    
    }   


    {   
        std::cout << "test erase item from std::set using reverse_iterator" << std::endl;

        std::set m;
        for(int i = 0 ; i < 5 ; i++)
        {   
            m.insert(i+1);
        }   

        std::set::iterator it; 
        std::set::reverse_iterator ri; 

        for(ri = m.rbegin() ; ri != m.rend() ; ) 
        {   
            if(*ri == 2 || *ri == 4)
            {   
                m.erase(  (++ri).base()  );  
            }   
            else
            {   
                ri++;
            }   
        }   
    
        for(it = m.begin() ; it != m.end() ; it++)
        {   
            std::cout << *it << std::endl;
        }   
    }   

}

 

你可能感兴趣的:(c++)