One Point Solution

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 17 April 2011

Outrageous: for_each #define

Posted on 17:45 by Unknown
When people think of C++, over-verbose is not usually the first thought. But it actually can really get so when dealing with the STL. Things like .begin() and .end() are really good at breaking your willing suspension of disbelief when you want to believe that C++ is a language that is there to help you :). The c++0x standard will improve c++ in many ways, but I fear it does not really enough new ways to save the problem of typing .begin() and .end().

Here is my problem. You have a set of pair<int, pair<int,int> > . That sort of STL abuse happens just about any time you are in a programming contest and wish to avoid having to make your own class with three elements and tie breaking comparisons. The pair and set templates are very useful when implementing some algorithms. The set template can function as a priority queue and also as a range tree if you know how to extract juice out of it. The thing is that it happens too frequently, that I need to iterate through the elements of a set of some complex data structure.

set<pair<int,pair<int,int> > > s;
for(set<pair<int,pair<int,int> > >::iterator q = s.begin(); q!=s.end(); q++) {
cout<< q->first <<", " << q->second.first << ", " << q->second.second<<endl;
}


If that did not induce you a headache, it is because you have been over exposed to c++. I love c++, so I consider it a good thing, but things can always be better.

The one million question is why does not c++0x seem to introduce anything close to Java's for(:) syntax, which allows you to iterate through containers very easily. I really, really dislike defines, but for some reason, c++ uses them as an excuse not to implement some features, because supposedly you can make them yourself by abusing macros. Well, in this case, I am starting to think that a macro is the cleanest solution. That is the reason I have begun typing it during topcoder matches, and I think I will add it to my template by default and do something to make a script remove it from my submission when it is not in use. The for_each macro I am using is as follows:

#define for_each(s,v) for(typeof((v).begin()) s=(v).begin(); s!=(v).end(); s++)


Most special thing is the use of the g++ extension typeof(). It may eventually get removed from g++, but that is probably going to coincide with the day c++0x auto keyword gets added. (auto q=v.begin() is even better).

The previous code becomes much clearer:

set<pair<int,pair<int,int> > > s;
for_each(q,s) {
cout<< q->first <<", " << q->second.first << ", " << q->second.second<<endl;
}


The benefit is that for_each should work with every STL container. But you would need a different macro if you want to, for example, iterate a vector in reverse order.
Email ThisBlogThis!Share to XShare to Facebook
Posted in c++, implementation, stl | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • TopCoder SRM 557 - finally
    SRM 557 Explanation for division 1 Easy and match recap. Explanations for div2 easy and div2 medium. It feels like it has been ages since t...
  • SRM 589 Editorial
    I have finished writing the editorial for TopCoder SRM 589: http://apps.topcoder.com/wiki/display/tc/SRM+589 . As you most likely noticed. L...
  • SRM 590 recap and editorial
    Another week another Topcoder match. Not a great day. I had a bad flu and still do. Div1 500: The one with Xor Given a list of cards with nu...
  • SRM 546: relief
    I figured I should post something about this SRM. I've been very busy these weeks because the semester is ending and I tried to win a t-...
  • SRM 526: The killing wait for results
    While I wait for results, here is my perspective on this algorithm contest. It began with issues, it had to be postponed 15 minutes. TC has ...
  • SRM 554 div1 hard: TheBrickTowerHardDivOne
    Link to problem statement We got infinitely many bricks of dimensions 1x1x1 and C different colors. Count the number of towers of size 2x2...
  • SRM 533: Div1 500 MagicBoard explanation
    Finally solved it. It is a nice problem that is worth explaining in a post. You have a grid/board of at most 50x50 cells. Some cells contain...
  • Member SRM 505: Part 1
    So, let me explain a couple of problems from a Topcoder Member SRM that I wrote and never got an editorial. BTW, it was the last member SRM....
  • ListedLinks 2012-02-10
    Saturday Morning Breakfast Cereal comics: Grace Hopper's ghost That Oracle engineer blog post Oracle would really not like anyone to se...
  • Codeforces "Good bye 2013" round
    So it was a special round for coders of both divisions, problems ranged from the super easy problem A to the super difficult problems E,F,G....

Categories

  • acm
  • algorithm
  • answers
  • arenaplugin
  • badday
  • behindthescenes
  • bugs
  • c++
  • censorship
  • codechef
  • codeforces
  • contests
  • crocchamp
  • editorial
  • editorial.srm
  • embarrassing
  • explanation
  • gcj2013
  • gmp
  • goodday
  • google
  • googlecodejam
  • greed
  • groklaw
  • health
  • html
  • httpseverywhere
  • implementation
  • ipsc
  • ispc
  • java
  • kawigiedit
  • kindagoodday
  • lamebook
  • languages
  • lego
  • listedlinks
  • marathon
  • nasa
  • offtopic
  • ouch
  • postmortem
  • postportem
  • practical
  • probably_not_a_good_tip
  • problemsetting
  • programming
  • python
  • quora
  • rant
  • recap
  • slightlygoodday
  • snippet
  • srm
  • stl
  • strategy
  • swerc
  • tco
  • tco12
  • tco13
  • tco2012
  • tco2013
  • ternarysearch
  • topcoder
  • tricks
  • ubuntu
  • uva
  • vjass
  • vkcup
  • wc3
  • zinc

Blog Archive

  • ►  2014 (1)
    • ►  January (1)
  • ►  2013 (141)
    • ►  December (14)
    • ►  November (8)
    • ►  October (13)
    • ►  September (11)
    • ►  August (14)
    • ►  July (15)
    • ►  June (13)
    • ►  May (13)
    • ►  April (12)
    • ►  March (11)
    • ►  February (11)
    • ►  January (6)
  • ►  2012 (94)
    • ►  December (5)
    • ►  October (6)
    • ►  September (8)
    • ►  August (6)
    • ►  July (3)
    • ►  June (5)
    • ►  May (8)
    • ►  April (10)
    • ►  March (20)
    • ►  February (16)
    • ►  January (7)
  • ▼  2011 (51)
    • ►  December (7)
    • ►  November (12)
    • ►  October (5)
    • ►  September (1)
    • ►  August (3)
    • ►  July (4)
    • ►  June (3)
    • ►  May (7)
    • ▼  April (3)
      • Outrageous: for_each #define
      • Member SRM 503 - Div2 250, Div2 900
      • Member SRM 503 - KingdomXCitiesandVillages
    • ►  March (2)
    • ►  February (1)
    • ►  January (3)
  • ►  2010 (9)
    • ►  December (4)
    • ►  October (1)
    • ►  June (1)
    • ►  May (1)
    • ►  January (2)
  • ►  2009 (1)
    • ►  December (1)
Powered by Blogger.

About Me

Unknown
View my complete profile