-6

我正在用 C++ 编写一个员工数据库程序。我想为每个员工生成一个唯一的员工编号,但我没有成功,有人可以帮助我,哦,请发布与 Turbo C++ Borland 而不是 Visual C++ 相关的代码。希望尽快收到你们的来信。如果不是代码,请告诉我一个算法来完成这项工作。

4

2 回答 2

2

从 1 开始,每次增加。这是一个单线程版本:

unsigned long long int unique_id()
{
    static unsigned long long int n = 0;
    return ++n;
}

对于线程安全版本,请改用 anstd::atomic<unsigned long long int>或添加互斥锁。

于 2012-10-30T16:59:37.797 回答
-1

一个非常简单的唯一 ID 生成器

class UniqueIDGenerator{
   private:
      static unsigned long uniqueId = 1000ul;   //Starting the IDs with 1000

   public: 
      static unsigned long generateUniqueId(){
         return uniqueId++;
      }
};

像这样使用它

int empId = UniqueIDGenerator::generateUniqueId();
于 2012-10-30T16:59:50.893 回答