O
Owen Brydon
Hi,
Is the following code legal? g++ 3.2 barfs on it, although it seems
fine to me.
#include <map>
using std::map;
int main()
{
map<int,int> i;
i[0]=6;
i[54]=2;
map<int,int>::iterator it=i.begin();
while(it!=i.end())
{
if(it->first==0) it=i.erase(it);
else it++;
}
return 0;
}
The error message from g++ is:
tmp.cpp:13: no match for `std::_Rb_tree_iterator<std:air<const int,
int>,
std:air<const int, int>&, std:air<const int, int>*>& = void'
operator
/usr/include/c++/3.2/bits/stl_tree.h:184: candidates are:
std::_Rb_tree_iterator<std:air<const int, int>, std:air<const
int,
int>&, std:air<const int, int>*>&
std::_Rb_tree_iterator<std:air<const
int, int>, std:air<const int, int>&, std:air<const int,
int>*>:perator=(const std::_Rb_tree_iterator<std:air<const int,
int>,
std:air<const int, int>&, std:air<const int, int>*>&)
As a workaround, is it valid to do the following, or does erasing the
iterator 'tmp' from the map (below) render the iterator 'it' invalid?
#include <map>
using std::map;
int main()
{
map<int,int> i;
i[0]=6;
i[54]=2;
map<int,int>::iterator it=i.begin();
while(it!=i.end())
{
if(it->first==0)
{
map<int,int>::iterator tmp=it;
it++;
i.erase(tmp);
}
else it++;
}
return 0;
}
Regards,
Owen Brydon
Is the following code legal? g++ 3.2 barfs on it, although it seems
fine to me.
#include <map>
using std::map;
int main()
{
map<int,int> i;
i[0]=6;
i[54]=2;
map<int,int>::iterator it=i.begin();
while(it!=i.end())
{
if(it->first==0) it=i.erase(it);
else it++;
}
return 0;
}
The error message from g++ is:
tmp.cpp:13: no match for `std::_Rb_tree_iterator<std:air<const int,
int>,
std:air<const int, int>&, std:air<const int, int>*>& = void'
operator
/usr/include/c++/3.2/bits/stl_tree.h:184: candidates are:
std::_Rb_tree_iterator<std:air<const int, int>, std:air<const
int,
int>&, std:air<const int, int>*>&
std::_Rb_tree_iterator<std:air<const
int, int>, std:air<const int, int>&, std:air<const int,
int>*>:perator=(const std::_Rb_tree_iterator<std:air<const int,
int>,
std:air<const int, int>&, std:air<const int, int>*>&)
As a workaround, is it valid to do the following, or does erasing the
iterator 'tmp' from the map (below) render the iterator 'it' invalid?
#include <map>
using std::map;
int main()
{
map<int,int> i;
i[0]=6;
i[54]=2;
map<int,int>::iterator it=i.begin();
while(it!=i.end())
{
if(it->first==0)
{
map<int,int>::iterator tmp=it;
it++;
i.erase(tmp);
}
else it++;
}
return 0;
}
Regards,
Owen Brydon