Thread: Distinct doubles

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Strangely I got the exact same result with posted code, std::set and vector+sort+unique (I just replaced random with rand). The latter method in particular caused a lot of thrashing (?) and I almost didn't wait it out.

    I suggest taking it easy on the OP, as he seems to be moving on and learning just fine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anon
    Strangely I got the exact same result with posted code, std::set and vector+sort+unique (I just replaced random with rand).
    As I noted in posts #7 and #15, std::map should be used instead of std::set, and the std::unique() generic algorithm is not applicable here. On the other hand, such changes would not change the time complexity of the algorithms involved.

    Quote Originally Posted by anon
    I suggest taking it easy on the OP, as he seems to be moving on and learning just fine.
    I feel that ArlexBee-871RBO should have:
    • Stated clearly the goal of the algorithm instead of stating it partially.
    • Linked to the source of the implementation instead of vaguely stating that "found this block of code on the web".
    • Posted the equivalent example using the standard library.
    • Posted the relevant performance comparisons from the start.

    The first point would have avoided getting incorrect suggestions and a correction to use std::map instead of std::set from the start. The second point could have more directly led to a explanation of the algorithm. The third point would have avoided grumpy's assumption that perhaps ArlexBee-871RBO did not really know how to use the standard library correctly in this case. The fourth point would make it more convincing that there was an attempt to actually measure to see if the performance was good enough.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There surely has been some misunderstanding.

    OP, it might be indeed hard to determine how good you really are from the initial posts, so there's no reason to be offended. But I think we've established now that you have a clue
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #19
    Registered User
    Join Date
    Mar 2007
    Posts
    25
    Quote Originally Posted by laserlight View Post
    I did, by stating that "it appears to be using the very property of strings being strings of characters to organise the strings into a tree structure". A trie is "a tree for storing strings in which there is one node for every common prefix". I even suggested what you now conclude as "one way to use the algorithm for doubles would be to consider a double to be an 8 character string and use the algorithm directly".


    As I noted earlier, the correct standard container for your purposes would be std::map, not std::set.


    If you use std::vector, then you should only use std::sort() and not std::unique(). I gave you the std::unique() suggestion because you only mentioned counting the number of distinct elements. I do not think that there is a standard algorithm to perform the count of the distinct groups, but it does only take a single pass after the range is sorted. Ultimately, the time complexity will be the same as for std::map, so if you need something asymptotically faster, neither will suffice.

    You are right. Going back and reading your comments now makes sense, which tells me you did have a clue as to what the algorithm was doing. I didn't know what the algorithm was, and you failed to give me a name for the algorithm, so I had no idea what you were saying. Up until then the only thing that I was familiar with was an ordinary binary tree.

    I actually haven't tried to solve my problem using std::map, but I will run a test to see what I get. I'll try to post my final code and compare it with the std version. Judging how std::set compares with the digital search trie, I figured it would be slow too.

  5. #20
    Registered User
    Join Date
    Mar 2007
    Posts
    25
    Quote Originally Posted by laserlight View Post
    I feel that ArlexBee-871RBO should have:
    • Stated clearly the goal of the algorithm instead of stating it partially.
    • Linked to the source of the implementation instead of vaguely stating that "found this block of code on the web".
    • Posted the equivalent example using the standard library.
    • Posted the relevant performance comparisons from the start.

    The first point would have avoided getting incorrect suggestions and a correction to use std::map instead of std::set from the start. The second point could have more directly led to a explanation of the algorithm. The third point would have avoided grumpy's assumption that perhaps ArlexBee-871RBO did not really know how to use the standard library correctly in this case. The fourth point would make it more convincing that there was an attempt to actually measure to see if the performance was good enough.
    The reason I didn't post my actual problem that I was trying to solve and my goal was because I knew I would get flamed by some for asking help with something that I had almost no clue as to how to solve without showing some lines of code first.

    I had a rough idea of what was needed, and that was to find the distinct number of doubles. That was a starting point for me. Because I needed the best performence, I Googled to find something that defeats the std::set. I had a rough idea of what the original algorithm that I post was doing, and I managed to modify it to work with doubles. But I had a sense that it might be doing something wrong because I didn't know what it was, so I thought I post here.

    I figured the code I posted would make sense to some because it's a complete program. It's not just some random block of code that doesn't run. Anyway...

    I'll post some comparison code onceI finish up with the digital trie and figure out which is the fastest.

  6. #21
    Registered User
    Join Date
    Mar 2007
    Posts
    25
    Quote Originally Posted by anon View Post
    There surely has been some misunderstanding.

    OP, it might be indeed hard to determine how good you really are from the initial posts, so there's no reason to be offended. But I think we've established now that you have a clue
    Yes, I can agree with that. There was some misunderstandings. And I wasn't offended, really. I just can't stand to see people treat others so harshly when there is no real reason for it, and I'm not afraid to bite back when someone treats me like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting a list of squared Doubles
    By dnguyen1022 in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2009, 01:24 PM
  2. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  3. How many distinct values?
    By George2 in forum Tech Board
    Replies: 2
    Last Post: 05-03-2008, 09:08 AM
  4. Passing array of doubles to ATL COM dll
    By wakeup in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2006, 02:53 AM
  5. ints and doubles problem
    By iLLiCiT in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 03:42 PM