STL? sort

This is a discussion on STL? sort within the C++ Programming forums, part of the General Programming Boards category; hi there, i have this problem: i need to sort an array of objects so that an overall quality of ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    STL? sort

    hi there,
    i have this problem:

    i need to sort an array of objects so that an overall quality of the array to be maximum.
    -the quality of the array depends on the order the points are in the array.
    - the arrays i am supposed to sort are small (3-7 members)

    for example: a={A B C D E} is the array and f(a) is the quality function.
    Code:
      f(a)=f(A)*f(A,B)*f(A,B,C)*f(B,C,D)*f(C,D,E)*f(E)
    is there a way i can use the STL sorting algorithms to do the sorting? or at least some of it?

    I will appreciate even a hint about the right direction to be followed

    Right now, i am generating all possible combinations and checking them all, so it "must" be a better way to do it than this. reducing the amount of computation to be done by chosing the A so that f(A) is best does not work, as there are cases with better f(a) which have a worse-than-optimal f(A).
    thank you

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The STL sort algorithms can be used with a custom sort function, but the function needs to accept two arguments, i.e. the two elements to be compared, and then return true or false. So, you would have to develop your own function to determine which element would produce the overall lower f(a).
    Last edited by 7stud; 12-04-2005 at 04:45 AM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    but how can i overcome the fact that i can compute f(a) only after i "finished" sorting? maybe there is something "more powerfull" in STL that i do not know about?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You can use the STL's next_permutation function to help you go through all the permutations, but I cannot think of a way to avoid checking all permutations through the STL.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    thank you daved. i read something about this function and it seems it might help a lot if i manage to get a good and fast comp object function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Sort Algorithm
    By f1player in forum C++ Programming
    Replies: 9
    Last Post: 10-10-2008, 01:12 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Min, max, sort in vector STL
    By codeguy in forum C++ Programming
    Replies: 7
    Last Post: 02-29-2008, 08:06 AM
  4. STL sort throw exception?
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 05:34 AM
  5. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM

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