Thread: max element in vector

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    max element in vector

    Hello,

    is it possible to use the stl MAX_ELEMENT algorithm to find the max value in a vector of vectors.
    Say I have the following 5 x 3 matrix represented in a vec. of vec.

    1 4 5
    3 5 8
    2 9 5
    8 0 2
    3 4 7

    where each row is a vector, so we have 5 vectors within a vector.

    I want to find the MAX_ELEMENT along the 1st, 2nd or 3rd column.
    For the 1st one the result is 8, the 2nd is 9, the 3rd is 8.

    any idea?

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, iterate over each column and apply max_element.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You'd have to define a custom iterator which iterates through each row. It would be a lot less work to just forego the max_element algorithm alltogether.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not if you use a Boost iterator adaptor and a Lambda object. Then it becomes a matter of 10 minutes, if you have to look up the documentation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM