STL: - get to know the STL and *use* it - if you're not comfortable with templates, get comfortable with them before the contest because the error messages when compiling can be mind-boggling - the most useful STL structures are string<> and vector<> - have a good reference available for the STL - put "using namespace std;" at the top of any file that uses the STL - the "c_str()" method of the string class can be very useful at times - for online references, check out http://www.sgi.com/tech/stl/ (click through to "Table of Contents"; they call the string class "basic_string") - and for the C++ streams library references look at http://www.cplusplus.com/ref/iostream/ - the STL contains sorting functions which are very useful. They are defined as sort(iterator, iterator). To sort a vector of integers, do the following: vector myVec; // get some stuff into myVec sort(myVec.begin(), myVec.end()); You can also use this on a standard array: int array[128]; // fill the array sort(array, array + 128); // give beginning/ending addresses Sorting and searching: - If you don't feel like using the STL's sort function, there is always the standard function qsort() (which requires you to provide a comparator function of a certain signature): int compare_ints(const void *a, const void *b) { int *x = (int *)a; int *y = (int *)b; return *y - *x; // essential thing is to return <0, 0, or >0 } // then use this... int array[128]; // fill the array qsort(array, 128, sizeof(int), compare_ints); - The function bsearch() has a similar signature to qsort(), but it does a binary search on an *already sorted* array. Geometry: - I will often make a point class for geometry problems with some key operator overloads for adding, subtracting, multiplying, dividing, and I/O of the class for geometry problems: class Point2d { public: double x, y; Point2d(double a, double b) { x = a; y = b; } /* operator+ and operator- allow you to use the class * like a 2-d vector like this: * Point2d p1(4, 3), p2(1, 7); * Point2d p3 = p2 + p1; // calls p2.operator+(p1); */ Point2d operator+(const Point2d &p) const { return Point2d(x + p.x, y + p.y); } Point2d operator-(const Point2d &p) const { return Point2d(x - p.x, y - p.y); } Point2d operator*(double m) const { return Point2d(x * m, y * m); } Point2d operator/(double m) const { return Point2d(x / m, y / m); } /* These functions are useful for reading data in from * the problem's input stream, and writing it out for * output or (more likely) debugging purposes. This * allows: * cin >> p1 >> p2; * cout << p1 << ", " << p2; * Note that these are declared "friend" functions and * are defined outside of the class (since they are not * really class member functions). They are declared * "friend" just in case we decide to make the data * members (x & y) private or protected. */ friend istream &operator>>(istream &is, Point2d &p); friend ostream &operator<<(ostream &os, const Point2d &p); }; // elsewhere, in the .cpp file (*not* in a .h file, unless we're // using templates), we define the input/output functions: istream &operator>>(istream &is, Point2d &p) { is >> p.x >> p.y; // depends on the format of the input // don't forget this -- enables chaining of input return is; } ostream &operator<<(ostream &os, const Point2d &p) { // this will output it as "(x, y)"; but you can do it however // you want... os << "(" << p.x << ", " << p.y << ")"; // don't forget this -- enables chaining of output return os; } - note that you can write a lot of small functions inline with your definition in the .h file; this can save typing. However, be careful not to define functions outside of the class in the .h file; if you include the .h in multiple .cpp files, this can result in linker errors (multiple definitions). - I normally make a cross-product function that operates on two Point2d objects... or you can put it in the class itself (but it still deserves separate mention) double cross(Point2d a, Point2d b) { return a.x * b.y - a.y * b.x; } General C++ things: - If you ever make a class which allocates (uses "new") as part of its data structure, always make sure to make a copy constructor to avoid getting a "shallow copy": MyClass(const MyClass &mc) { // do everything necessary to copy mc into *this // (including allocation of required memory) } and also an operator= function *which checks to make sure you're not trying to assign something to itself (e.g. "a = a;"): MyClass &operator=(const MyClass &mc) { if (this == &mc) { return *this; } // check for "a = a;" // otherwise, do all the stuff to assign mc into *this // return a reference to this object for chained // assignment like "a = b = c;" return *this; } Input and Output: - using the printf() function from C is extremely useful for programming contest output (rather than cout with its modifiers for number field width, etc.). However, if you're concerned about using printf() and cout together (I have read but never experienced that using them together can cause problems with what actually gets output to stdout) then you can use sprintf() and print something into a buffer that you then output with cout, like the following example. Or you could always use printf() and never use cout. But I don't recommend always using cout. double answer = 7.0 / 3.0; char buffer[128]; sprintf(buffer, "The answer is %0.3f miles.\n", answer); cout << buffer; - Know how to use the printf (and scanf) flags (man printf; man scanf) - Very handy: use the istream function "istream.getline(char *, int len)" for processing data one line at a time. This way you don't have to keep searching for newlines in unstructured input. Make sure you don't have a buffer that's too small, though! - If you're using the STL, there is a non-class function also called getline(), with signature getline(istream &, string &), which does not require you to specify the length of the buffer (which makes it even more useful than the istream version of getline). - If you're going to be doing a lot of string concatenation, I have heard that the "rope" class is better than the "string" class (in STL), but I have not found rope nearly as easy to work with. - The IOstream library provides an "istringstream" for doing formatted input from a string (there is also ostringstream for doing formatted output to a string). This can be useful if you want to use getline() (either one) to get input a line at a time, and then put that line into an istringstream object to get at each field without having to parse it yourself.