Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Maps and function objects problem, with code

This is a discussion on Maps and function objects problem, with code within the C++ Programming forums, part of the General Programming Boards category; Code: /* * failure.cpp * * Created on: Sep 27, 2011 * Author: mike * * failure.cpp - A program ...

  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,794
    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.
    wildcard_seven likes this.
    "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,208
    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%.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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
    21,451
    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().
    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

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-02-2009, 11:00 PM
  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

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