![]() |
STD-DISCUSSION |
Subject: Sequential containers. Method erase with the iterator returned by end()
From: Vladimir Grigoriev (vlad.moscow_at_[hidden])
Date: 2020-04-29 09:52:31
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
STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org