[RESOLVED] std::set<int> can't find negative value
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] std::set<int> can't find negative value

  1. #1
    Join Date
    Mar 2005
    Posts
    99

    [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?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,422

    Re: std::set<int> can't find negative value

    Quote Originally Posted by nice_guy_mel View Post
    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

  3. #3
    Join Date
    Mar 2005
    Posts
    99

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,422

    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,422

    Re: std::set<int> can't find negative value

    Quote Originally Posted by nice_guy_mel View Post
    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

  6. #6
    Join Date
    Mar 2005
    Posts
    99

    Re: std::set<int> can't find negative value

    Quote Originally Posted by Paul McKenzie View Post
    ???

    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!

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: std::set<int> can't find negative value

    Quote Originally Posted by nice_guy_mel View Post
    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.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: std::set<int> can't find negative value

    Quote Originally Posted by Lindley View Post
    The phrase which has me worried is "I put them at the end."
    ditto.

    Quote Originally Posted by Lindley View Post
    I'm actually really curious what you were doing.
    ditto.

    Please humour our curiosity.
    Is your question related to IO?
    Read this C++ FAQ LITE 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.

  9. #9
    Join Date
    Mar 2005
    Posts
    99

    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

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,422

    Re: [RESOLVED] std::set<int> can't find negative value

    Quote Originally Posted by nice_guy_mel View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center