Finding address pointing to a value

This is a discussion on Finding address pointing to a value within the C++ Programming forums, part of the General Programming Boards category; I'd like to get the maximum of these values, and then locating the address of the result. How do I ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I'd like to get the maximum of these values, and then locating the address of the result. How do I do this?

    Code:
    max( max(player[0].pts, player[1].pts),
                max(player[2].pts, player[3].pts));
    I also tried using max_element, but I can't figure out how to do it on structs.
    Last edited by 843; 04-07-2011 at 11:07 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by 843
    I also tried using max_element, but I can't figure out how to do it on structs.
    Overload operator< for your struct type, or define a predicate and pass it to max_element.

    Once max_element returns an iterator to the max element, you dereference the iterator and take the address of the result.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    What's a predicate? Could you please give me a quick example?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by 843
    What's a predicate?
    A function, or function object, that returns a bool depending on some condition typically related to some property of one or more objects, e.g., if an object is "less than" another object.

    Quote Originally Posted by 843
    Could you please give me a quick example?
    Yes:
    Code:
    #include <algorithm>
    #include <iostream>
    
    struct Player
    {
        int points;
    };
    
    // Predicate to compare Player objects by points.
    bool compareByPoints(const Player& x, const Player& y)
    {
        return x.points < y.points;
    }
    
    int main()
    {
        using namespace std;
        Player players[4] = {{2}, {4}, {1}, {3}};
        cout << max_element(players, players + 4, compareByPoints)->points << endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 07:56 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. finding out ip address?
    By chiatello in forum Linux Programming
    Replies: 3
    Last Post: 08-07-2003, 06:18 AM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM

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