VincentLascaux

Members
  • Content count

    44
  • Joined

  • Last visited

Community Reputation

112 Neutral

About VincentLascaux

  • Rank
    Member
  1. The C/C++ 'Did You Know?' Thread

    Quote:Original post by Jingo Quote:Original post by Mayrel int i = 0; std::cout << (++i + ++i + ++i) << std::endl; Did you know that that code actually can validly print "I am a pink elephant hello hello 12345"? Did you know that it's wrong? The code can only output one number (and it's hard to guess which ones it can :)). You can have outputs 1, 2, 3 and 6. Not sure about 4 and 5, and probably nothing higher than 6
  2. Quote:Original post by snk_kid No you don't have to use reserve & third explicit loop in this case e.g. typedef std::vector< std::vector<int> > foo_vec; for(foo_vec::size_type x = 0, m = cells.size(); x < m; ++x) for(foo_vec::size_type y = 0, n = cells[x].size(); y < n; ++y) std::vector<Point> neighbours(8, Point(x, y)); But again i question what use "neighbours" has when the second inner loop is finished "neighbours" is destroyed by scope. Come on ! It's obvious that it's not real code ! He won't put 8 times the same point in a vector (I assume... :)) In a general true situation, you would put different points in the neighbours vector and wouldn't be able to use this constructor. But you are right that giving us such small and unrealistic code doesnt help. The compiler could just see that the loop is doing nothing and not put it in the exe.
  3. Use reserve for(int x=0;x<(int)cell.size();x++) { for(int y=0;y<(int)cell[x].size();y++) { std::vector<Point> neighbours; neighbours.reserve(8); for(int i=0;i<8;i++) { Point p(x,y); neighbours.push_back(p); } } } Can you show the code that you claim faster using dynamic pointers?
  4. How to patch a patch program?

    Create a batch file called patcher which only purpose is to launch the patcher. It will contain a single line : the name of the patcher executable file, which should contain a version number. When updating the patcher, you just have to change the batch file to make it launch the new version of the executable.
  5. java recursive problem

    Quote:Original post by Anonymous Poster Quote: 2)If n is divisible by 3 or 4,he can substract n%10+(n/10)%10 Is being divisible by 4 any different than being even? I don't see how you can reconcile this with the first rule. Also, (n/10)%10 is always going to be 0 if n is an integer. So you may as well make this rule simply by n%10. If it is divisible by 4, then (I think), you might apply any of the two rules. If it is divisible by 20 or 30 (divisible by 2, 3 and 5 or 2, 4 and 5), then you might apply any rule you want. Also, (n/10)%10 has no reason to be 0. Take 120 for example. 120/10 = 12, 12%10 = 2... (n*10)%10 is always 0 (with a multiplication). n%10 + (n/10)%10 is the sum of the two last digits of the number written in base 10. So if n=142, the value is 4+2=6 Quote: Note that f(z) is the same regardless of how you arrived at z (hint: use dynamic programming). Yes, that's a good idea... But the 3 rules seem to make the number small very quickly. So I'm pretty sure that computing f(z) for any z from 0 to n is very time consumming. The basic recursive idea would be f(z) = min(f(apply_rule_1(z)), f(apply_rule_2(z)), f(apply_rule_3(z), z). (apply a rule only if possible of course). The bad thing with that is that is may recompute several times the same f(z). So you could just store the result, and compute f(z) only if it has not already been computed. Something like int computed[n+1]; //init all values to -1 int f(int z) { if(computed[z] >= 0) return computed[z]; int min = z; int after_rule; if(can_apply_rule_1(z) && (after_rule = f(apply_rule_1(z)))<min) min = after_rule; if(can_apply_rule_2(z) && (after_rule = f(apply_rule_2(z)))<min) min = after_rule; if(can_apply_rule_3(z) && (after_rule = f(apply_rule_3(z)))<min) min = after_rule; return computed[z] = min; }
  6. Inexplicable problem...

    Why dont you post the code !? All what you have to do is take your code, remove as many lines as possible keeping the same error (remove unrelated functions, unrelated comments, try removing classes, variables...). Once you have posted this 20 line long code which contains the error, we will be able to help you. Now, why do we never see people doing that? Because if you do that you will probably find the error by yourself! (yes, you're right, it's easier to say "hey, can you guess what my problem is, and then try to solve it")
  7. Since decimal numbers may not be representable in binary format, your assertion is not true. For example (I may be wrong), 0.6 is represented as 0.100110011001100110011001100... in binary. Since the double with store "1100" more times, it will be closer to the exact value (that contains an infinity of "1100") that floats.
  8. Unique ID manager

    Can you explain why you need UID and why the memory location of the object itself is not a valid enough identifiant ?
  9. Collision detection algorithm suggestions

    Lets check if the point (x,y) is in the rectangle (xa1, ya1, xb1, yb1). It is if x in [xa1 xb1] and y in [ya1 yb1]. Now your rectangle moves, let's say according to a starting position and a speed (xa1(t) = xa1(0) + xv*t, same for other coordinates). You now want to check if it exists one time t so that x in [xa1 xb1] and y in [ya1 yb1] x >= xa1(0) + xv*t => t <= (x-xa1(0))/xv x <= xa2(0) + xv*t => t >= (x-xa2(0))/xv (same with y). Like that, you determine the interval of time during wich the point will be inside the rectangle. If you want to check the collision of two rectangles, you can say that they interact iif one of both points of the second one collide with the first rectangle. If both rectangles are moving, you can rewrite the equations and see that it is like if only one of them was moving, with a speed equal to the sum of the two speeds.
  10. Both versions are dangerous because they dont use RAII. std::vector< std::vector<int> > my2darray(xsize, std::vector<int>(ysize)); does
  11. Parameterization by return type

    I doubt it. Anyway, Comeau says no (http://www.comeaucomputing.com/tryitout/) int* myint = NewObject<int>(); works though
  12. Memory leak problem

    What is m_tuples ?
  13. C++ - 2 unrelated questions

    In some case, throwing an exception is more adapted than doing an assert. What if the Circle class is built reading a script or configuration file. You would be happy to be able to output a "The configuration file is ill formated : the radius of a circle must be positive". You should not catch the exception after calling Circle(5,5,5), because you have in mind that this can't throw. If you don't know the reason of the error, there is no point in handling it because you won't solve the problem.
  14. Fast multikey map container.

    Sorry, I think I made a mistake between multikeys and multivalues. The obvious way is to keep a third table ID -> names. It will probably not be so good depending on how often you remove elements by ID.
  15. Fast multikey map container.

    Why dont you use a hashmap of lists ?