Thread: Finding address pointing to a value

  1. #1
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    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, 08: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