What is the meaning of the "<" operator?

c++

Solution

`T->key < key` is a condition. It will evaluate to either `true` or `false`. If it evaluates to `true`, `dir` will get value `1`, otherwise it will get value `0`.

int dir = T->key < key;

is short form for writing

int dir;
if(T->key < key)
    dir = 1;
else
    dir = 0;

When a `boolean` is assigned to an `int`, it gets the value `0` or `1` corresponding to `false` or `true`.

Problem

In the following code for a binary search tree: ``` template <class TKey> class bst<TKey>::node *bst<TKey>::insert(node *T, TKey &key) { if (T == NULL) { T = new node; T->key = key; } else if (T->key == key) { cout << "key " << key << " already in tree" << endl; } else { int dir = T->key < key; T->link[dir] = insert(T->link[dir], key); } return T; } ``` I'm confused what the line ``` int dir = T->key < key; ``` is doing. I could understand `int dir = T->key`, although of course that wouldn't make sense, but I've not seen the `<` operator used in that way before. Any clues?

Original source

Related problems