Category Archives: IT

How to use struct as a key of std::map in C++, and its pitfall

We can use a structure and a class, which are defined by myself, as key of std::map.

If we want to do so, we need define comparison operator < for defined structure or class, because std::map stores data  as binary tree. If we don’t define <, computer can not decide which one is bigger or smaller.

I show the sample code below. In this sample, a structure, which has an int and char[100] as its member, works a key of std::map.

Continue reading