G
Generic Usenet Account
I am seeing some unexpected behavior while using the STL "includes"
algorithm. I am not sure if this is a bug with the template header
files in our STL distribution, or if this is how it should work.
For your benefit, let me first quote from the STL Standard Reference
(Stepanov & Lee):
template <class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1
last1,InputIterator2 first2, InputIterator2 last2);
template <class InputIterator1, class InputIterator2, class Compare>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp);
includes returns true if every element in the range [first2, last2) is
contained in the range [first1, last1). It returns false otherwise.
At most ((last1 - first1) + (last2 - first2)) * 2 - 1 comparisons are
performed.
I have been able to reproduce my problem with a much more simplified
example. Here's my sample source code:
////////////////// SRC BEGIN /////////////////////////
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
#define TRACE_STMT cout << "\nLine: " <<__LINE__ << endl;
int
main()
{
vector<int> v1;
vector<int> v2;
for(int i = 1; i < 7; i++)
v1.push_back(i);
TRACE_STMT
copy(v1.begin(), v1.end(), ostream_iterator<int> (cout, " "));
for(int i = 3; i < 6; i++)
v2.push_back(i);
vector<int>::iterator itor;
for(itor = v1.begin(); itor != v1.end(); itor++)
if(!includes(v2.begin(), v2.end(), itor, itor))
v1.erase(itor);
TRACE_STMT
copy(v1.begin(), v1.end(), ostream_iterator<int> (cout, " "));
return 0;
}
////////////////// SRC END /////////////////////////
I am expecting elements 3, 4 & 5 to be erased from vector v1, but that
is not happening. The only thing "wierd" about this example is that
"first2" and "last2" have the same value here. However, the STL
Reference Manual does not mention that "first2" and "last2" have to be
distinct.
Any pointers will be appreciated....
Thanks,
Gus
algorithm. I am not sure if this is a bug with the template header
files in our STL distribution, or if this is how it should work.
For your benefit, let me first quote from the STL Standard Reference
(Stepanov & Lee):
template <class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1
last1,InputIterator2 first2, InputIterator2 last2);
template <class InputIterator1, class InputIterator2, class Compare>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp);
includes returns true if every element in the range [first2, last2) is
contained in the range [first1, last1). It returns false otherwise.
At most ((last1 - first1) + (last2 - first2)) * 2 - 1 comparisons are
performed.
I have been able to reproduce my problem with a much more simplified
example. Here's my sample source code:
////////////////// SRC BEGIN /////////////////////////
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
#define TRACE_STMT cout << "\nLine: " <<__LINE__ << endl;
int
main()
{
vector<int> v1;
vector<int> v2;
for(int i = 1; i < 7; i++)
v1.push_back(i);
TRACE_STMT
copy(v1.begin(), v1.end(), ostream_iterator<int> (cout, " "));
for(int i = 3; i < 6; i++)
v2.push_back(i);
vector<int>::iterator itor;
for(itor = v1.begin(); itor != v1.end(); itor++)
if(!includes(v2.begin(), v2.end(), itor, itor))
v1.erase(itor);
TRACE_STMT
copy(v1.begin(), v1.end(), ostream_iterator<int> (cout, " "));
return 0;
}
////////////////// SRC END /////////////////////////
I am expecting elements 3, 4 & 5 to be erased from vector v1, but that
is not happening. The only thing "wierd" about this example is that
"first2" and "last2" have the same value here. However, the STL
Reference Manual does not mention that "first2" and "last2" have to be
distinct.
Any pointers will be appreciated....
Thanks,
Gus