Thread: macro for looping through a templated map

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    macro for looping through a templated map

    Is it possible to make a macro to loop through any kind of maps?

    Normally, it's
    Code:
    map<int, string>::iterator it;
    for(it = somemap.begin(); it != somemap.end(); it++){
       // do schtuff
    }
    For an xml class I have, I've actually made some looping macros which replaces
    Code:
    int i;
    for(i = 0; i < xmlnode.size(); i++){
       xmlnode& tec = xmlnode[i];
       // do schtuff
    }
    into
    Code:
    xmlloop(xmlnode)
       // do stuff with "tec" as a xmlnode&
    xmlloopend
    So yeah, can the same be done with maps without having to specify their types? that is, I don't want:
    Code:
    maploop(int, string, somemap)
       // do stuff with it, which is an iterator..
    maploopend

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could take a look at Boost.Foreach. In the next version of C++, there will probably be a range based for loop that would make this redundant.

    Of course, you could also consider applying an appropriate generic algorithm instead of using a convenience macro.
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Shouldn't std::for_each() work too?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Shouldn't std::for_each() work too?
    That is what I mean by "applying an appropriate generic algorithm", though I hope that an even more appropriate algorithm is chosen, if feasible (or maybe a std::map specific algorithm should be used). The Boost.Foreach documentation notes that this "forces us to move our logic far from where it will be used", unless lambda expressions are used.
    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
    Jan 2005
    Posts
    108
    I see.. even with combining a #define macro with for_each it will still need to know the type.. I guess it's probably good for type checking.

    anyways thanks!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by underthesun
    even with combining a #define macro with for_each it will still need to know the type..
    If you are going to restrict the usage to std::map (or perhaps to standard library containers in general), then this is not necessary since value_type typedef in the scope of the std::map would provide the necessary type information. However, you would then loose the covariance feature provided by BOOST_FOREACH, although you gain in convenience.
    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

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Hmm.. care to explain that last bit? How exactly do I use value_type for automatically getting an iterator type given a map?

    Code:
    map<int, int> themap;
    
    themap::iterator it; // <-- is this where I can use value_type somehow?
    for(it = themap.begin(); it != themap.end(); it++){
       blah();
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... looks like I am wrong. Since one cannot use a function template, the value_type macro is not useful, since one still needs to provide the type of the container to the macro in order to access it.
    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. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM