Results 1 to 2 of 2

Thread: max_element() in C++

  1. #1
    ashley_black is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    4

    Default max_element() in C++

    Hi. I am last year Bsc(IT) student. I have C++ programming subject in my syllabus. Currently I have little bit knowledge of C++. But I don’t understand max_element () function. So I want detailed info on max_element () function. If anybody haves any info then please let me know that.

  2. #2
    Join Date
    Feb 2010
    Posts
    46
    Rep Power
    0

    Default

    Syntax:
    #include <algorithm>
    forward_iterator max_element( forward_iterator start, forward_iterator end );
    forward_iterator max_element( forward_iterator start, forward_iterator end, BinPred p );


    The max_element() function returns an iterator to the largest part in the range [start,end).

    If the binary predicate p is given, then it will be used in place of the < operator to find out the largest element.

    For example, refer the following code to find out the largest integer in an array and the largest character in a vector of characters:

    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;
    When run, the above code displays this output:
    Code:
     Max element in array is 9
       Max element in the vector v is d

Similar Threads

  1. max_element () in C++
    By ScottWright in forum Programming
    Replies: 2
    Last Post: 07-09-2010, 02:05 PM
  2. Using max_element () in C++
    By EvansMitchell in forum Programming
    Replies: 2
    Last Post: 03-08-2010, 02:10 PM

Tags for this Thread

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