C++ Logo

std-discussion

Advanced search

Sequential containers. Method erase with the iterator returned by end()

From: Vladimir Grigoriev <vlad.moscow_at_[hidden]>
Date: Wed, 29 Apr 2020 17:52:31 +0300
I did not found in the standard where there is said what will occur if to use as the argument of the method erase the iterator returned by the method end.
 
For example this program ends abnormally.
 
#include <iostream>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v = { 1 };
    
    auto it = v.erase( v.end() );
    
    std::cout << std::boolalpha << ( it == v.end() ) << '\n';
 
    return 0;
}
 
While this program
 
#include <iostream>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v = { 1 };
    
    auto it = v.erase( v.end(), v.end() );
    
    std::cout << std::boolalpha << ( it == v.end() ) << '\n';
 
    return 0;
}
 
finishes normally.
 
For the class template std::string for the method
 
basic_string& erase(size_type pos = 0, size_type n = npos);
 
there is written clear that the argument that specifies the position can equal to the value of size().
 
But looking through sections describing sequential containers I did not find where something is said what happens when the passed iterator is the iterator returned by end().
 
With best regards
(Vlad from Moscow)
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com

Received on 2020-04-29 09:55:34