[RESOLVED] std::set<int> can't find negative value
Using unmanaged C++ in Visual Studio 2005. I am using a std::set<int> collection to hold some integer values. I recently found a bug where only the positive values were able to be checked and not the negative values.
If I put the value 111 in the collection, then do a .find(111), then it finds the value as expected. But if I put the value -111 in the collection, then do a .find(-111), then it just returns the end() pointer. Is this the way the std::set collection is supposed to work?
Re: std::set<int> can't find negative value
Quote:
Originally Posted by
nice_guy_mel
Using unmanaged C++ in Visual Studio 2005. I am using a std::set<int> collection to hold some integer values. I recently found a bug where only the positive values were able to be checked and not the negative values.
If I put the value 111 in the collection, then do a .find(111), then it finds the value as expected. But if I put the value -111 in the collection, then do a .find(-111), then it just returns the end() pointer. Is this the way the std::set collection is supposed to work?
Let's see the code. Describing what you're doing doesn't tell us what you're actually doing.
Regards,
Paul McKenzie
Re: std::set<int> can't find negative value
Never mind, I forgot the set is ordered, so all the negative values are supposed to be at the front of the collection. I put them at the end, so when I did the .find, it probably stopped the search since the first value was positive, and therefore there was no point searching further for a negative value.
Re: std::set<int> can't find negative value
In addition, it isn't that hard to create a simple main() program to test things:
Code:
#include <set>
#include <iostream>
using namespace std;
int main()
{
set<int> IntSet;
IntSet.insert( -111 );
set<int>::iterator it = IntSet.find(-111);
if ( it != IntSet.end() )
cout << "I found -111";
else
cout << "I didn't find -111";
}
Does this work?
Regards,
Paul McKenzie
Re: std::set<int> can't find negative value
Quote:
Originally Posted by
nice_guy_mel
Never mind, I forgot the set is ordered, so all the negative values are supposed to be at the front of the collection.
???
What difference does this make where the number is in the set? You are supposed to use find() to search for an element. How are you searching your set for the element?
Regards,
Paul McKenzie
Re: std::set<int> can't find negative value
Quote:
Originally Posted by
Paul McKenzie
???
What difference does this make where the number is in the set? You are supposed to use find() to search for an element. How are you searching your set for the element?
Regards,
Paul McKenzie
Don't worry about it, I panicked and posted before taking the time to actually investigate properly. I'm impressed with the fast response though, thanks for looking out for us Paul!
Re: std::set<int> can't find negative value
Quote:
Originally Posted by
nice_guy_mel
Never mind, I forgot the set is ordered, so all the negative values are supposed to be at the front of the collection. I put them at the end, so when I did the .find, it probably stopped the search since the first value was positive, and therefore there was no point searching further for a negative value.
The phrase which has me worried is "I put them at the end." It should not be possible to control where in the container a given value ends up----no matter what order you insert things in, the container controls its own internal representation. Plus, iterators have const attributes which should prevent you from modifying elements already in the container unless you do something silly like a const_cast.
I'm actually really curious what you were doing.
Re: std::set<int> can't find negative value
Quote:
Originally Posted by
Lindley
The phrase which has me worried is "I put them at the end."
ditto.
Quote:
Originally Posted by
Lindley
I'm actually really curious what you were doing.
ditto.
Please humour our curiosity.
Re: [RESOLVED] std::set<int> can't find negative value
Pretty silly in the end, but here it goes.
I set a breakpoint in my code where the collection was being searched for -111. At the time I only had positive values in the collection, one of which was 111. So I used the data menu (the one that pops up when you mouse over a variable), to change the value in the collection from 111 to -111. When that failed, I thought something wonky was going on, so I posted about it here. If I had taken more time to realize that the collection was sorted, then I would have realized that you can't change a value on the fly like that and expect the Find() method to work correctly. When I added the value the proper way with Insert(-111), then the Find(-111) worked as expected.
Definitely not the sexy iterator manipulation you were probably thinking of :)
Re: [RESOLVED] std::set<int> can't find negative value
Oh, okay. Yeah, don't change values in the debugger unless you're absolutely sure of what you're doing.
Re: [RESOLVED] std::set<int> can't find negative value
Quote:
Originally Posted by
nice_guy_mel
So I used the data menu (the one that pops up when you mouse over a variable), to change the value in the collection from 111 to -111. When that failed, I thought something wonky was going on, so I posted about it here.
Since set more than likely uses a tree to store the data, changing the data value would mangle the tree data structure, and thereby making the set basically unusable.
Regards,
Paul McKenzie