Thread: Maps and function objects problem, with code

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    86

    Maps and function objects problem, with code

    Code:
    /*
     * failure.cpp
     *
     *  Created on: Sep 27, 2011
     *      Author: mike
     *
     *  failure.cpp - A program to read in words from stdin, tally their frequency, and add frequencies to
     *  tally up the total number of words
     */
    
    #include <map>
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    /*
     * A function object that tallies the value elements of map pairs
     */
    class Frequency_works {
    	private:
    		int* total;
    	public:
    		Frequency_works(int& x) : total(&x) {}
    		void operator() (const pair<const string, int>& elem) {*total += elem.second;}
    };
    
    /*
     * A function object that tallies the value elements of map pairs
     */
    class Frequency_fails {
    	private:
    		int total;
    	public:
    		Frequency_fails(int x = 0) : total(x) {}
    		void operator() (const pair<const string, int>& elem) {
    			total += elem.second;
    			//cout << total << ' '; // For testing
    		}
    		int getTotal() {return total;}
    };
    
    /*
     * Reads words into a map from stdin, and accumlates their frequency
     */
    void get_words ( map <string,int>& words) {
    
    	string tmp;
    
    	// While there is input, read from stdin
    	while (cin >> tmp && tmp != "DONE") {
    		words[tmp]++;  // Record word and frequency
    	}
    }
    
    
    int main () {
    
    	int total = 0;
    
    	Frequency_works f1(total);
    	Frequency_fails f2;
    
    	map<string, int> w;
    	get_words (w);
    
    	// Tally and print total words, using function object of class Frequency_works
    	for_each (w.begin(), w.end(), f1);
    	cout << "Total words (Frequency works): " << total << '\n';
    
    	//Tally and print total words, using function object of class Frequency_fails
    	for_each (w.begin(), w.end(), f2);
    	cout << "Total words (Frequency fails): " << f2.getTotal() << '\n';
    
    
    	return 0;
    
    }
    OUTPUT (using the words I give as I describe how to use below)

    Code:
    Total words (Frequency works): 5
    Total words (Frequency fails): 0

    To use the program, just feed it a list of words, preferably with certain words repeated, on stdin. End with the word DONE. Like so.

    Code:
    ./executable
    box
    box
    hammer
    hammer
    nail
    DONE
    When you are finished, you can tell me why Frequency_fails doesn't do its job as a function object.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Probably because of how the Frequency_fails object is passed into the for_each function. The Frequency_works object increments the total variable in main but the Frequency_fails one has an internal counter and is probably passed into the function by copy so when the for_each function call ends, the value in the object back in the main function has not been altered ...that's my guess.

    Try this:
    Code:
    //Tally and print total words, using function object of class Frequency_fails
    f2 = for_each (w.begin(), w.end(), f2);
    cout << "Total words (Frequency fails): " << f2.getTotal() << '\n';
    The for_each function returns the function object but in your case you were ignoring the return value from the function.

    [edit]I just tested that and it seemed to make that part work.[/edit]
    Last edited by hk_mp5kpdw; 09-27-2011 at 02:25 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    f2 is passed by value to the function. So the counting is performed in a copy of f2.

    There is a reason that for_each() returns the function passed as an argument.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An alternative solution (good to know!) is to use boost::ref, eg:
    for_each(w.begin(), w.end(), boost::ref(f2));

    Also, in case your compiler supports it, you should be using the free functions std::begin and std::end instead of those member functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    Beautiful. Thank you everyone. You guys are the best. I may be back if a search doesn't readily tell me the difference between "free function" std::begin and std::end and the member functions, because I have no idea what that's about. Otherwise, thanks again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I simply mean is that instead of
    for_each(w.begin(), w.end(), f2);
    ...you should do...
    for_each(std::begin(w), std::end(w), f2);

    With appropriate included headers, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    You were just about to explain the advantage of this, when....the doorbell rang?

    Seriously, I tried a search, and I'm batting zero. Not that I know what I'm looking for.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just because it allows possible optimizations in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not know about the optimisations part, but I see them mainly as tools to make it easier to write function templates that operate on arrays in addition to containers that provide member begin() and end().
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  2. First code for Classes and Objects
    By CrKrJak in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2009, 11:24 PM
  3. Maps and Function Pointers
    By cyreon in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2009, 12:00 AM
  4. Retrieving the exit code of CWinThread objects
    By mynickmynick in forum Windows Programming
    Replies: 4
    Last Post: 10-07-2008, 08:10 AM
  5. Problem with more than 1 C function in code
    By tuxson in forum C Programming
    Replies: 3
    Last Post: 07-22-2008, 06:27 PM