how does map take care invalid range for erase()?

N

newbie

Does standard say anything about this situation? or it is an undefined
behavior?

Thanks

------------------------------
map<int, int> test;
test[1] = 1;
test[2] = 2;
test[3] = 3;

test.erase(test.find(1), test.find(2)); // basically this is an
invalid range to erase.

map<int, int>::iterator pos = test.begin();
test.erase(pos, --pos); // another example of invalid range.
----------------------------
 
A

Andre Kostur

Does standard say anything about this situation? or it is an undefined
behavior?

Thanks

------------------------------
map<int, int> test;
test[1] = 1;
test[2] = 2;
test[3] = 3;

test.erase(test.find(1), test.find(2)); // basically this is an
invalid range to erase.

That's invalid? Perhaps you meant find(2) then find(1) ?
map<int, int>::iterator pos = test.begin();
test.erase(pos, --pos); // another example of invalid range.
----------------------------


Yep, they're both Undefined Behaviour. It's the programmer's
responsibility to ensure that the ranges passed to erase() (as in: the
first iterator is "less than" or equal to the second iterator.
 
N

newbie

Does standard say anything about this situation? or it is an undefined
behavior?

------------------------------
map<int, int> test;
test[1] = 1;
test[2] = 2;
test[3] = 3;
test.erase(test.find(1), test.find(2)); // basically this is an
invalid range to erase.

That's invalid? Perhaps you meant find(2) then find(1) ?
Right, I meant the other way. test.erase(test.find(2), test.find(1));
Yep, they're both Undefined Behaviour. It's the programmer's
responsibility to ensure that the ranges passed to erase() (as in: the
first iterator is "less than" or equal to the second iterator.

Thanks for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top