-1

假设你有一个被调用的结构体bundle,它由string对象组成。没有关于一个包将包含多少个字符串的准确信息,您需要为每个包生成标识号,以便区分它们。

例如,两个捆绑包有 5 个字符串对象,而这两个对象中只有四个是通用的。

注意 1:我需要一个识别号,因为在此过程中我遇到了很多捆绑包,其中一些具有完全相同的字符串。

注 2:我在 c++ 上工作,据我所知,c++ 中没有可散列或类似的东西。

How can we generate identification number ?

我想到的唯一解决方案是将字符串对象连接成一个包。我认为没有其他解决方案。也许以另一种格式或数据结构表示字符串可以更容易生成 id。

4

1 回答 1

-1

您可以使用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;
}
于 2018-12-07T11:24:56.953 回答