-1

Assume that you have a struct called bundle and it consists of string objects. There is no accurate knowledge about how many strings a bundle will contain, and you need to generate identification number for each bundle so that you can differentiate them.

For example, two bundles have 5 string objects and only four of them are common for these two objects.

Note 1: I need an identification number because I face a lot of bundles in the process and some of them have exactly same strings.

Note 2: I work on c++, and as far as I know, there is no hashable or something like that in c++.

How can we generate identification number ?

The only solution which came to my mind is to concatenate string objects in a bundle. I think that there is no any other solution. Maybe representing strings in another format or a data structure can make it easier to generate an id.

Goktug
  • 855
  • 1
  • 8
  • 21
  • 1
    not completely clear what is the problem. If your "bundle" is a `std::vector` then maybe all you need is to use its `operator==`... can you show some code? – 463035818_is_not_an_ai Dec 07 '18 at 11:03

1 Answers1

-1

You could use a static int counter:

#include <iostream>
static int counter = 0;
struct bundle
{
    bool operator==(bundle& other){ return this->id == other.id; }

    int id = counter++;
    std::string a, b, c, d, e;
};

int main()
{
    bundle b1, b2, b3, b4, b5;
    std::cout << b1.id << ' ' << b5.id << std::endl;    // 0 4
    std::cout << (b1 == b5) << std::endl;               // 0
    b1 = b5;
    std::cout << (b1 == b5) << std::endl;               // 1
    return 0;
}
Fubert
  • 80
  • 5