PrevNext

(Optional) Writing Generic Code

Authors: Aryansh Shrivastava, Benjamin Qi

Writing code that can easily be reused or extended.

Resources
Aryanshintegrated here to enhance the USACO Guide
LCPP

This section is not complete.

Feel free to file a request to complete this using the "Contact Us" button.

Ben - I was too lazy to check whether all the code compiles :/

C++

Generic code is an important concept in informatics. Of course, as all concepts go, you may dodge generic code and continue to write in a hyper-specific style. As such, begin by questioning purpose.

Why write generic code?

Generic code is adaptable, meaning that it can be put to use immediately in many ways without major changes. It can be reused, extended, and even versioned powerfully to save time. Time is of essence in informatics, where I refer to both algorithmic time complexity and coding time.

Even if you are writing a very specific data structure or algorithm, to truly grasp it, a good contemplation is "Can I generalize what I have learned to a broader class of problems?" Answer this by then attempting to generalize. That said, before you proceed, I issue the below warning:

Warning!

Generic code can easily assist black-boxing, where you write a snippet of code such that you either forget its meaning or have no idea what it means in the first place. This is a common pitfall, and you should avoid this as much as possible if you truly want to grasp a concept in informatics.

Modern C++, a groundbreaking language in the current informatics scene, indeed has several builtin features to support and streamline generic code. Here, we will cover the basic important ones you will definitely want to add to your coding arsenal.

Warning!

Although you are not allowed to use pre-written code for USACO contests, the conventions covered in this module still apply when typing up a template from scratch.

Classes

Classes are by far the most important utility in extensible code. If you ever want to write a data structure that has several member functions to process stored data, classes are for you. Of course, classes can have public and private sections. For instance, consider a class human, which maintains several relevant member functions.

1#include <bits/stdc++.h>
2using namespace std;
3
4class human {
5 private: //intrinsic properties
6 int body_temperature, temper;
7 string name;
8 public: //external reactions
9 string name() { //what do other people call him
10 return name;

But how do we actually use (and reuse) this class data structure we have created? We do this by creating instances of this class, concretely known as objects.

Below is an example instance of the human class, a human object named Sal.

1human Sal;

Of course, we will want to initialize any object of this human class with its fundamental attributes (body_temperature and temper from the above), but the problem here is that we are unable to access them directly; they are private and remain uninitialized.

To partially alleviate this, we can initialize variables in the class declaration itself:

1#include <bits/stdc++.h>
2using namespace std;
3
4class human {
5 private: //intrinsic properties
6 int body_temperature = 98, temper = 25;
7 string name = "Sal";
8 public: //external reactions
9 string name() { //what do other people call him
10 return name;

This gives us something to work with, and we can now create Sal in main() as well as call his member functions (instantiated directly from the base human class).

1int main() {
2 human Sal;
3 cout << Sal.name() << " feels " << Sal.feeling()
4 << " and is " << Sal.emotion() << "\n";
5}

To completely solve this, we might be forced to make these variables public, but instead we can be more clever and write a constructor function for this class, essentially a function that is automatically called whenever an instance of the class is created. Constructors are useful when we want to prevent modification to variables we create but be able to initialize them for use, making them naturally the most viable option for proprietary software companies.

We can create a constructor for the human class and require initialization of the variables body_temperature and temper, which gives us some control over Sal's intrinsic properties as they are initialized. The overall code becomes:

1#include <bits/stdc++.h>
2using namespace std;
3
4class human {
5 private: //intrinsic properties
6 int body_temperature, temper;
7 string name;
8 public: //external reactions
9 human(string _name, int _body_temperature, int _temper) {
10 name = _name;

This is immediately very extensible if we wish to create multiple instances of human, all with their own initial properties. In fact, we can be even more general by creating an external function condition that easily states feelings and emotions for us without having to be rewritten.

1void condition(human h) {
2 cout << h.name() << " feels " << h.feeling() << " and is " << h.emotion();
3}
4
5int main() {
6 human Sal("Sal", 98, 25), Bob("Bob", 100, 9), Joe("Joe", 85, 35);
7 condition(Sal), condition(Bob), condition(Joe);
8}

As one very specific but useful note on constructors, when we wish to merely initialize properties, we can adopt an alternative declaration that executes significantly faster than the first; using the argument name as the variable itself also becomes permissible and is guaranteed defined behavior:

1human(string name, int body_temperature, int temper) :
2name(name), body_temperature(body_temperature), temper(temper) {}

Structs

Structs are useful when we care less about keeping private properties and more about having just a general reusable data structure. This means everything is public in a struct by default, and a human struct, along with the reformatted constructor above, would look like:

1struct human {
2 int body_temperature, temper;
3 string name;
4 human(string name, int body_temperature, int temper) :
5 name(name), body_temperature(body_temperature), temper(temper) {}
6 string feeling() { //answers "how do I feel"
7 if(body_temperature >= 97 && body_temperature <= 99) return "good";
8 else return "bad";
9 }
10 string emotion() {

Immediately, our struct is easier to manage given its open nature. It is more integrated with the surrounding code, and we can do manipulations like:

1human Sal("Sal", 98, 25);
2condition(Sal); //get Sal's initial condition
3Sal.name = "Sally" //Sal's friends sometimes call him Sally
4Sal.body_temperature = 102; //Sal gets sick
5Sal.temper = 40; //he develops bad temper
6condition(Sal); //now, get Sal's new condition

Finally, we can choose to discard the constructor altogether and opt for an initializer list based on the order of the declaration of intrinsic variables. In the human case, the variables in order are body_temperature, temper, and name, so we can remove the constructor and opt for an initializer list like:

1human Sal{98, 25, "Sal"};

Needless to say, all of this enables very clean initialization and manipulation of classes and structs, integral to generic code.

Templates

The human class example, though well-defined, was mostly intended to serve as an example of the versatility of classes and structs. We now switch to something simpler. Imagine a three-dimensional point in space as the following struct, where we create some sample instances P1 and P2

1struct pt {
2 int x, y, z;
3} P1{1, 2, 3}, P2{3, 4, 5}; // we can make quick instances right before the ;

But what if we wanted to make a P3 point with coordinates as doubles? We would then be forced to create a secondary point struct pt1:

1struct pt { int x, y, z; } P1{1, 2, 3}, P2{3, 4, 5};
2struct pt1 { double x, y, z; } P3{1.1, 2.2, 3.3};

This might not seem too bad immediately, but imagine having to create structs like this over and over again just to accommodate various type changes. We need something better.

Lo and behold, the template to the rescue. We can use the format template<...> to specify the specific templating conditions and then simply define the struct normally. In particular, if we use a class T, we achieve:

1template<class T> struct pt { T x, y, z; };
2pt<int> P1{1, 2, 3}, P2{3, 4, 5};
3pt<double> P3{1.1, 2.2, 3.3};
4pt<long long> P4{9223372036854775807, 9223372036854775807, 9223372036854775807};

It would be narrow-minded to think that templates are in any way limited to classes and structs. They can be used with functions and much more. For example,

1template<class T> bool ckmin(T& a, const T& b) {
2 return b < a ? a = b, 1 : 0; }

will set aa equal to min(a,b)\min(a,b), and return true if the value of aa changes.

One interesting use case is that of the size of various containers. The size member function of a container usually returns a type incompatible with integer, but we can easily write a templated function to fix this, handling all class types T of container at once:

1template<class T> inline int sz(T container) {
2 return (int)container.size();
3}

You may apply this as sz<vector<int>>(v) on some vector<int> v, but since C++11, functions (but not classes or structs until C++14 and C++17) can actually infer template arguments, meaning that we can simply use sz(v)!

What if we wanted to put multiple arguments in a template to handle multiple classes? Consider the below secondary pair comparator struct as an example:

1struct CPS {
2 template<class T, class U>
3 bool operator() (const pair<T, U>& a, const pair<T, U>& b) {
4 return make_pair(a.second, a.first) < make_pair(b.second, b.first);
5 }
6};

And in this design, since the template arguments both apply to only a function, they can be easily inferred! For instance, declaring a set of pair<double, int> in C++11 is as easy as set<pair<double, int>, CPS>.

What constitutes what other kinds of types we can put in templates? As a general rule of thumb, until C++17, the types valid in template arguments are only classes and fundamental types, of which only classes can be directly inferred in many cases by functions.

In fact, we can even have templates that take a variable number of arguments, known as variadic templates, or templates within templates, known as nested templates, both of which are beyond the scope of this basic exposition but can be found here and here.

As a fun fact, just as templates give us so much control over the generality of the language, a lot of the C++ standard in itself is written generically with templates under the hood.

Type Aliases With using

typedef is now rather outdated (though still used by some) because it is more or less just an annoying version of using with frustrating semantics, so we will not cover it here.

using is a fascinating keyword, frequently used to simplify namespace prefixing when applicable. For instance, statements like

1using namespace std;

actually allow us to use an entire namespace. Of course, since using namespace std is frequently limited to the competitive programming community and looked down upon otherwise, we can use using to invoke better simplifications.

Suppose that a lot of my code usesstd::cout, which I find frustrating to type. I can write

1using std::cout;

and then just live with cout. But what if I was using strings and wanted to type neither std::string nor string? I could use using twice to fix this:

1using std::string; //unnecessary if already using namespace std
2using str = string; //use str as an alias for string

Or golfed into a single statement:

1using str = std::string; //str is an alias for std::string directly

We can make even more aliases, with even aliases within aliases (see ll), for environments where speed is key, such as competitive programming:

1using namespace std;
2using ll = long long;
3using str = string;
4using pii = pair<int, int>;
5using pll = pair<ll, ll>;
6using vi = vector<int>;

Warning!

If you use too many type aliases or macros then your code will become unreadable to anyone besides yourself!

Finally, we can take using to the next step and invoke templates! For instance, if we want to be able to write ar<int, 6> instead of std::array<int, 6>, we write:

1template<class T, int SZ> using ar = std::array<T, SZ>;

As another example, if we want to use ai to mean integer array, we can make constructions like ai<6> work as well:

1template<int SZ> using ai = std::array<int, SZ>;

In general, it is important that using declarations have strong scope guarantees, meaning that they will not work outside of their defined scope. To use declarations everywhere in the program, they must be invoked in global scope. But, if we want to be clever and create a reusable struct and just specify template arguments internally, we always have the option of:

1struct pt {
2 using T = int;
3 //within this scope, T is an alias for int
4 //just change this declaration to change T's meaning within this struct
5 T x, y, z;
6};

In case we want to be able to access the specific type T's alias meaning outside of pt, this too becomes very easy:

1struct pt {
2 using T = int;
3 T x, y, z;
4};
5
6int main() {
7 using U = pt::T;
8 //U becomes a copy of T from pt's scope
9 //U is now in the scope of main
10}

Macros

We end this section off with #define, which is used to define macros.

Resources
CPHsimple examples of macros, describes a common bug
GCCreference
GFG
LCPP

#define is essentially a crude find-and-replace that happens before compile time (in the preprocessor stage). In this sense, it is easy to use, where #define NAME VALUE would ideally find all instances of NAME in the code and replace them with VALUE.

1#define MOD 1e9 + 7
2
3int main() {
4 cout << MOD << "\n";
5}

This example defines MOD as 1e9+7 by find and replace. But without resorting to something as crude as #define, we could just as well do:

1const int MOD = 1e9 + 7;
2
3int main() {
4 cout << MOD << "\n";
5}

The takeaway is to avoid #define when possible. If you absolutely insist on using #define for some reason, though, use it with caution and remember to #undef for good scope control.

Of course, some competitive programmers use macros extensively. Some examples are presented below.

Pairs

1typedef pair<int,int> pi;
2#define mp make_pair
3#define f first
4#define s second

Pretty annoying to keep typing first and second (especially if you have nested pairs ...)

Vectors

1typedef vector<int> vi;
2#define sz(x) (int)x.size()
3#define all(x) begin(x), end(x)

It's generally a good idea to convert a size to a signed integer before doing anything with it to avoid cases like the following.

1vi x;
2cout << x.size()-1 << "\n"; // 18446744073709551615
3cout << sz(x)-1 << "\n"; // -1

all(v) makes sorting part or all of a vector a bit shorter.

1vi v{2,4,1,5,3};
2sort(1+all(v)); // {2,1,3,4,5}
3// this expands to sort(1+begin(v), end(v));
4sort(all(v)); // {1,2,3,4,5}

Note how 1+all(v) expands to 1+begin(v), end(v).

Preprocessing Logic

As far as preprocessor magic goes, we are in no way limited to #define butchery. Ever wanted to write a program that compiles in different ways depending on some initial conditions? We can use preprocessor directives like #if and #else, or #ifdef and #ifndef to allow for this.

For instance, I may want my struct pt to be two-dimensional in some cases, and I can control this simply:

1const bool d2 = 0; //change to true if you want the 2d case; otherwise, 3d
2
3template<class T> struct pt {
4 #if(d2)
5 T x, y;
6 #else
7 T x, y, z;
8 #endif
9};

Similarly, if we are not opposed to #define, we could use #ifdef and #ifndef to see whether or not a macro is defined via #define.

1//#define 2d //uncomment for 2d
2
3template<class T> struct pt {
4 #ifdef 2d
5 T x, y;
6 #else
7 T x, y, z;
8 #endif
9}

There are many clever applications of this, including versioning. Importantly, if we want code to run differently for different versions of C++, we can write:

1#if(__cplusplus < 201703L)
2 template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
3 assert(!(hi < lo));
4 return (v < lo) ? lo : (hi < v) ? hi : v;
5 }
6#endif

Namespaces

Finally, we can write our own namespaces to separate various functions. Within a namespace, we can have functions, variables, classes, and even more namespaces. Then, we can invoke using declarations to use the whole namespace.

1namespace test {
2 const string greeting = "hi";
3 namespace test1 {
4 const int time = 2;
5 } using namespace test1;
6 template<class T> struct my_ds {
7 T s;
8 void add(T x) { s += x; }
9 T get() { return s; }
10 };

These are fairly self-explanatory, but a more rare feature of C++11 is the inline namespace. Inline namespaces are not technically real namespaces but allow us to chunk up code and avoid having to use namespaces just to gain access.

1inline namespace test {
2 const string greeting = "hi";
3}

So why even have inline namespaces? We can use their features interestingly. For instance, suppose we had a feature in an old version (v1) of a program but now removed it in the new version (v2).

1namespace v1 {
2 const string buggy_feature = "bugs";
3 const string greeting = "hi";
4}
5
6inline namespace v2 {
7 //removed the buggy_feature from this new version
8 const string buggy_feature = "doesn't exist";
9 const string greeting = "hi";
10}

Now, not using a namespace will automatically use v2. But if we want access to v1 of the buggy_feature, we can simply write v1::buggy_feature. There you have it, simple and effective version control!

Module Progress:

Give Us Feedback on (Optional) Writing Generic Code!

PrevNext