-
August 5th, 2010, 10:03 AM
#1
[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?
-
August 5th, 2010, 10:07 AM
#2
Re: std::set<int> can't find negative value
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
-
August 5th, 2010, 10:10 AM
#3
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.
-
August 5th, 2010, 10:11 AM
#4
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
-
August 5th, 2010, 10:12 AM
#5
Re: std::set<int> can't find negative value
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
-
August 5th, 2010, 10:18 AM
#6
Re: std::set<int> can't find negative value
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!
-
August 5th, 2010, 10:21 AM
#7
Re: std::set<int> can't find negative value
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.
-
August 5th, 2010, 10:36 AM
#8
Re: std::set<int> can't find negative value
Originally Posted by Lindley
The phrase which has me worried is "I put them at the end."
ditto.
Originally Posted by Lindley
I'm actually really curious what you were doing.
ditto.
Please humour our curiosity.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 5th, 2010, 12:18 PM
#9
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
-
August 5th, 2010, 12:34 PM
#10
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.
-
August 5th, 2010, 01:56 PM
#11
Re: [RESOLVED] std::set<int> can't find negative value
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|