ordering vector elements based on heuristic values

This is a discussion on ordering vector elements based on heuristic values within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to order elements of a vector based on heuristic values. For example, if I have a ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

    ordering vector elements based on heuristic values

    Hi,
    I am trying to order elements of a vector based on heuristic values. For example, if I have a class, A, and a function,
    Code:
    int evaluate(A &input);
    that returns the heuristic value of A, I want to define a function that orders a vector of A based on their heuristic values -
    Code:
    vector<A> *order(vector<A> *input);
    What is the most efficient way of doing that?

    Thank you very much

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Code:
    std::sort(v.begin(),v.end(),evaluate);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Thank you very much. That works nicely.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I have a question though. It seems to me that the compare function that is passed into sort has to take two parameters by value. However, the data type that I am working with here is ~200 bytes in size, and I want to avoid passing them around by value if possible. If I define the compare function to take parameters by reference, compilation fails. Is there any workaround or alternatives?

    Thank you.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    The function object that std::sort takes wants to know when one element is smaller than another. My mistake. What you want is this:
    Code:
    class heuristic_order
    {
         bool operator() (const A & left, const A & right)     //this'll inline.
         {
                return evaluate(left) < evaluate(right);
         }
    };
    
    // . . . . . . . . . 
    
    std::sort(v.begin(),v.end(),heuristic_order());
    And if references "won't compile," then something is wrong with your code (no offense). References are lovely. See how you can work in the above.

    *edit* reference problems might have to do with const-ness. Use non-constant references only when you know that you will have to modify the variable to achieve your goal (in a particular scope). Otherwise (like in comparison and "heuristic" algorithms), use constant references.
    Last edited by CodeMonkey; 08-16-2007 at 01:18 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Thank you very much, const-ness was indeed the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IRC-type logger with files based on three values
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 08-27-2008, 05:42 PM
  2. Replies: 1
    Last Post: 06-17-2008, 05:00 PM
  3. values into array based on coordinates
    By BabyWasabi in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2006, 07:48 PM
  4. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 08:42 AM
  5. Variable Names based on Variable Values
    By mrpickle in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2003, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21