Question: I am new to computer science and I guess this has something to do with hash function.

I have a class in cpp:

```
class Car{
    public:
    int fleetId;
    OwnershipEnum owner;
    int buildDate;
    bool hasFourDoors;

    ... with many other members
};

```

Now, my problem is, how to 'scientifically' generate an integer key based on the 4 members I list?

I can think of a simple way, `10000*int(fleetId) + 1000 * int(owner) + 100×int(buildDate) + int(hasFourDoors)`

I think ideally the key is continuous, so I can store all the Car objects in an array and use this generated key to DIRECTLY access the car object.

\*\*\*\*\* as per the comments: the cars are all different, there is no identical cars \*\*\*\*\*\*\*\*

\*\*\*\*\* these four members are static: they won't change after being created \*\*\*\*\*

Can anyone point me to the right solution here?

Thanks

Answer: 
You can build a method to get it, using `std::hash`:
Its just an easy example.

```
std::size_t CarHash(const Car& car)
{
    std::size_t h1 = std::hash<int>{}(car.fleetId);
    std::size_t h2 = std::hash<int>{}(int(car.owner));
    std::size_t h3 = std::hash<int>{}(int(car.isGoodCondition));
    std::size_t h4 = std::hash<int>{}(int(car.isBooked));
    return h1^(h2 << 1)^(h3 << 2)^(h4 << 3);
}

```
Let assume you want a key without collision and all field can take any value in their range, you'd have to use a sizeof(int) + sizeof(OwnershipEnum) + 2 bits key to prevent all collisions, or 66 bits. It can be considered acceptable and computed like this:

```
struct key {
    int fleetId;
    OwnershipEnum owner;
    char bools;

    key(Car _myCar) : 
        fleetId(_myCar.fleetId), 
        owner(_myCar.owner), 
        bools(!!_myCar.isGoodCondition & !!_myCar.isBooked << 1 ) 
    {}

    bool operator ==(key other) { return fleetId == other.fleetId && owner == other.owner && bools == other.bools; }
};

```

then test like this:

```
  Car first{1, (OwnershipEnum)2, false, true};
  Car second{1, (OwnershipEnum)2, false, true};
  Car third{8, (OwnershipEnum)1, true, true};

  key k1{first}, k2{second}, k3{third};

  std::cout << (k1 == k2) << "\n"; //true
  std::cout << (k2 == k3) << "\n"; //false
  std::cout << (k3 == k1) << "\n"; //false

```

full example program: <http://cpp.sh/2vv2a>

It's obviously a wrong assumption but you can try to reduce the key size if you can make all possible value for fleetId fit in 16 bits or shorten OwnershipEnum as for example