JOIN
 Select a Forum     Round Tables New Member Discussions News Discussions Algorithm Matches Marathon Matches NASA Tournament Lab TopCoder Cookbook High School Matches Sponsor Discussions Development Forums Design Forums Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings View: Flat (newest first)  | Threaded  | Tree Previous Thread  |  Next Thread Forums TopCoder Cookbook Algorithm Competitions - Rewriting Phase Using and Understanding C++ Macros
 Using and Understanding C++ Macros | Reply Problem:You want to understand programs written by C++ users, but they are using so many macros, and other abbreviations, that their solutions no longer look like normal C++ code, and seem impossible to understand.Or, you want to be like them, and use macros yourself.Note: this recipe is about abbreviations which use features available only in C++. See the "Writing Template" recipe for more abbreviations, which are useful for all languages.SolutionMost of people abbreviate the same things. Here are the most common abbreviations.Abbreviations for data types which occur most frequently in TopCoder problems:```typedef long long LL; typedef long double LD; typedef vector VI; typedef vector VS; typedef pair PII; ```There are two common versions of FOR macros:```#define FOR(k,a,b) for(typeof(a) k=(a); k <= (b); ++k) #define REP(k,a) for(int k=0; k < (a); ++k) ```or:```#define FOR(k,a,b) for(typeof(a) k=(a); k < (b); k++) ```Size macro:```#define SZ(x) ((int)((x).size())) ```Other popular abbreviations:```#define ALL(c) (c).begin(), (c).end() #define PB push_back #define MP make_pair ```Discussion:Of course, this is just one possible set of macro names (although probably the most common one), and you will see solutions where macros are named differently. For example, some people use lowercase names; using uppercase names for your macros agrees with the normal programming style, and improves readability by making obvious that something is an abbreviation, not a variable name. It is not obvious whether common programming style suggests using uppercase names for type name abbreviations (typedefs), but still, it improves readability. There are also people who use even shorter abbreviations (as REP(i,a) is too long for them, they use just fi(a)—the Cookbook does not recommend it, since it only helps with making your solution harder to read and challenge, and making those who want to learn from your submissions angry, there is almost no point in saving these 3 characters).As shown above, that there are two popular versions of FOR macros: one uses the closed range [a,b] while the other uses the range [a,b). Remember about that when challenging. Both have their advantages and disadvantages: [a,b) agrees with the convention used throughout STL and is usually faster to use, while using [a,b] makes it harder to have an off-by-one bug. Having both FOR and REP, as shown in the first version, gives advantages of both styles.Why so many coders use abbreviations for FOR loops anyway? The reason is that FOR loops usually appear several times in a TopCoder match, and if you save, for example, 3 seconds by using one, and there are 20 for loops in total in an SRM, you save a minute in total, which could be significant. Another reason to use them is that the normal for statement is bug prone: you could write e.g. for(int i=0; i
 Re: Using and Understanding C++ Macros (response to post by Eryx) | Reply Great - nothing to add :-)
 Forums TopCoder Cookbook Algorithm Competitions - Rewriting Phase Using and Understanding C++ Macros Previous Thread  |  Next Thread