macro for looping through a templated map

This is a discussion on macro for looping through a templated map within the C++ Programming forums, part of the General Programming Boards category; Is it possible to make a macro to loop through any kind of maps? Normally, it's Code: map<int, string>::iterator it; ...

  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
    22,170
    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.
    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
    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
    22,170
    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.
    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
    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
    22,170
    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.
    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

  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
    22,170
    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.
    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. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 06: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

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