generators.h

来自「Think in C++ 第二版源码」· C头文件 代码 · 共 64 行

H
64
字号
//: C21:Generators.h

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Different ways to fill sequences

#ifndef GENERATORS_H

#define GENERATORS_H

#include <set>

#include <cstdlib>

#include <cstring>

#include <ctime>



// A generator that can skip over numbers:

class SkipGen {

  int i;

  int skp;

public:

  SkipGen(int start = 0, int skip = 1)

    : i(start), skp(skip) {}

  int operator()() {

    int r = i;

    i += skp;

    return r;

  }

};



// Generate unique random numbers from 0 to mod:

class URandGen {

  std::set<int> used;

  int modulus;

public:

  URandGen(int mod) : modulus(mod) { 

    std::srand(std::time(0)); 

  }

  int operator()() {

    while(true) {

      int i = (int)std::rand() % modulus;

      if(used.find(i) == used.end()) {

        used.insert(i);

        return i;

      }

    }

  }

};



// Produces random characters:

class CharGen {

  static const char* source;

  static const int len;

public:

  CharGen() { std::srand(std::time(0)); }

  char operator()() { 

    return source[std::rand() % len];

  }

};



// Statics created here for convenience, but

// will cause problems if multiply included:

const char* CharGen::source = "ABCDEFGHIJK"

  "LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

const int CharGen::len = std::strlen(source);

#endif // GENERATORS_H ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?