question about std::distance
Jeffrey Holle
jeff.holle@verizon.net
Thu Sep 23 03:06:00 GMT 2004
Found my problem!
It was my ltEdgeDescriptor functor.
Instead of:
return s1.m_source < s2.m_source ||
(!(s1.m_source < s2.m_source) && s1.m_target < s2.m_target);
It needed to be:
return s1.m_source < s2.m_source ||
(!(s2.m_source < s1.m_source) && s1.m_target < s2.m_target);
Jeffrey Holle wrote:
> Found my alternate implement via:
> typedef map<vector EdgeData> > Edges.
> Would really like to know if I found a big bug in sgi stl or my bug,
> probably in my compare functor.
>
> Jeffrey Holle wrote:
>
>> On further investigation, it appears that the problem that I'm having
>> is related to my ltEdgeDescriptor functor. In calling either .count
>> or distance, this functor is called forever. It has been used before,
>> but only with map. It appears to be at least intolerant of duplicates.
>> There it is:
>> struct ltEdgeDescriptor : public
>> std::binary_function<DataEdge,DataEdge,bool>
>> {
>> bool operator() (const DataEdge& s1, const DataEdge& s2) const
>> {
>> return s1.m_source<s2.m_source || (!(s1.m_source<s2.m_source) &&
>> s1.m_target<s2.m_target);
>> }
>> };
>>
>> I copied this pattern from stl_pair.h, so I thought I was safe.
>> Can someone point out the problem here and hopefully sugguest an
>> alternate implementation?
>>
>>
>>
>> Jeffrey Holle wrote:
>>
>>> I'm using gcc v3.4.2.
>>>
>>> For the first time, I'm attempting to use std::distance and
>>> std::advance and am having trouble with at least std::distance.
>>>
>>> Basically, I'm trying to deal with duplicates.
>>> In the following code, I want to iterate thru just the non-duplicates.
>>> In my test case, edges has a size of 47 and there are two sets of 4
>>> duplicates each. So I want the for loop to execute 41 times.
>>>
>>> typedef std::multiset<DataEdge,ltEdgeDescritor> Edges;
>>> Edges edges;
>>>
>>> <populate edges>
>>>
>>> for(Edges::iterator iter=edges.begin();
>>> iter!=edges.end();
>>> advance(iter,distance(edges.lower_bound(*iter),edges.upper_bound(*iter)))
>>>
>>> {
>>> .....
>>> }
>>>
>>> What I find is that my program enters distance and doesn't leave.
>>>
>>> Am I somehow misusing distance?
>>>
>>> Can someone sugguest an alternate method for me to accomplish what
>>> I'm trying to do?
>>>
>>>
>>
>>
>
>
More information about the Gcc-help
mailing list