Thread: function efficiency

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    function efficiency

    How can I extend the following code for MAX(a, b, c), MAX(a, b, c, d) etc without writing out a new function?

    Code:
    double MAX(double M, double N)
    {
    
      if (M > N){return M;}
    
      else{return N;}
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    max_element() from <algorithm> returns an iterator to the maximum element in a sequence.

    Edit: If you insist on writing your own implementation, you probably want to express the input as a sequence in the same way that the Standard Library does, so you don't have to deal with variadic functions.
    Last edited by robatino; 07-03-2007 at 08:57 PM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    i don't understand what you mean

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    http://www.cppreference.com/cppalgor...x_element.html

    A variadic function is one that can take a variable number of arguments. For your purposes, you probably don't want to create one of those. Look at the example in the link above and then create a function that works the same way that max_element() does, or just use max_element(). max_element() is NOT variadic - it takes just two arguments (or three if you use the generalized version).

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM