For Programmers: Free Programming Magazines  


Home > Archive > VC STL > January 2006 > BUG? std::remove incorrect when value to be removed is in the sequence









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author BUG? std::remove incorrect when value to be removed is in the sequence
Igor Tandetnik

2006-01-25, 9:59 pm

std::remove algorithm takes a value to be removed by const reference.
This reference may refer to an element that is a member of the sequence
the algorithm works on. When this happens, the algorithm may produce
incorrect results.

Consider:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(1);

vector<int>::iterator newend = remove(v.begin(), v.end(),
v.front());
for (vector<int>::iterator it = v.begin(); it != newend; ++it) {
cout << *it << endl;
}
return 0;
}

It outputs

2
1

on both VC7.1 and VC8. It should output a single value 2.

I don't believe such usage is prohibited by the standard.

Bug report here:
http://lab.msdn.microsoft.com/Produ...2c-80b45c755ada

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com