Results 1 to 3 of 3

Thread: Max function in C++

  1. #1
    RodríguezBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    322
    Rep Power
    3

    Default Max function in C++

    Hi. I am last year BSc(I.T) student. In my last lecture sir give the assignment on max_element() function. I tried different solution but none of them worked out .any suggestion would be highly appreciated.

  2. #2
    WilsonMartin is offline Senior Member
    Join Date
    Dec 2009
    Posts
    319
    Rep Power
    3

    Default

    I suggest you to refer following example of max_element() function, which will give you something idea about the use of this function:
    Code:
    int array[] = { 3, 1, 4, 1, 5, 9 };
      unsigned int array_size = sizeof(array) / sizeof(array[0]);
      cout << "Max element in array is " << *max_element(array, array+array_size) << endl;
     
      vector<char> v;
      v.push_back('a'); v.push_back('b'); v.push_back('c'); v.push_back('d');
      cout << "Max element in the vector v is " << *max_element(v.begin(), v.end()) << endl;
    Output:
    Code:
       Max element in array is 9
       Max element in the vector v is d

  3. #3
    AndersonDiaz is offline Senior Member
    Join Date
    Dec 2009
    Posts
    311
    Rep Power
    3

    Default

    Prototype
    Max_element is an overloaded name; there are in fact two max_element functions.
    template <class ForwardIterator>
    ForwardIterator max_element(ForwardIterator first, ForwardIterator last);

    template <class ForwardIterator, class BinaryPredicate>
    ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
    BinaryPredicate comp);
    Description
    Max_element locates the largest element in the range [first, last). It returns the first iterate i in [first, last) such that no other iterate in [first, last) points to a value greater than *i. The return value is last if and only if [first, last) is an empty range.
    The two versions of max_element be different in how they define whether one element is less than another. The first version compares objects using operator<, and the second compares objects using a function object comp.

    The first version of max_element returns the first iterator i in [first, last) such that, for each iterate j in [first, last), *i < *j is false. The second version returns the first iterate i in [first, last) such that, for every iterate j in [first, last), comp(*i, *j) is false.

    Requirements on types
    For the first version:
    • ForwardIterator is a model of Forward Iterator.
    • ForwardIterator's value type is LessThan Comparable.
    For the second version:
    • ForwardIterator is a model of Forward Iterator.
    • BinaryPredicate is a model of Binary Predicate.
    • ForwardIterator's value type is convertible to BinaryPredicate's first argument type and second argument type.
    Preconditions
    • [first, last) is a valid range.
    Complexity
    Linear. Zero comparisons if [first, last) is an empty range, otherwise exactly (last - first) - 1 comparisons.
    Code:
    int main()
    {
      list<int> L;
      generate_n(front_inserter(L), 1000, rand);
      
      list<int>::const_iterator it = max_element(L.begin(), L.end());
      cout << "The largest element is " << *it << endl;
    }

Similar Threads

  1. Can’t function hot key
    By Garcíarobine in forum Networking Jargons
    Replies: 2
    Last Post: 06-12-2010, 11:02 AM
  2. erase() function in C++
    By RussellBarnes in forum Programming
    Replies: 1
    Last Post: 04-30-2010, 02:52 PM
  3. erase() function in C++
    By ScottWright in forum Programming
    Replies: 1
    Last Post: 03-13-2010, 10:39 AM
  4. How can I use the calculation () function in CPP
    By AllenBrown in forum Programming
    Replies: 1
    Last Post: 02-13-2010, 02:46 PM
  5. using The Loop Function
    By jack879 in forum Everything Else
    Replies: 0
    Last Post: 11-17-2008, 12:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48