Skip to content

A data structure for non-deterministic element selection in C++11

License

Notifications You must be signed in to change notification settings

Dakostu/GrabBag.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GrabBag

GrabBag is a data structure for non-deterministic element selection in C++11.

By using templates and the C++ STL, this class aims to be as versatile of a data structure as possible.

The GrabBag concept in a nutshell

Insert an element into a GrabBag, grab a random element out of the GrabBag.

How to Use

GrabBag is defined in a single header file, a simple include directive will suffice.

For a more in-depth explanation, check out the "documentation" folder which includes a thorough class explanation with code examples.

Simple Usage Example

A most simple use case for a GrabBag would be simulating a lottery game.

The following example shows the usage of GrabBag inside such a lottery program.

#include "src/GrabBag.h"
#include <iostream>
#include <set>

// Select 6 numbers out of 49 
int main() {
    
    GrabBag<int> enaLottoPool;
    std::set<int> pickedNumbers;
    
    for (int i = 1; i <= 49; ++i)
        enaLottoPool += i;
    
    for (int j = 0; j < 6; ++j)
        pickedNumbers.insert(enaLottoPool.grab());
    
    // Output Picked Numbers
    std::cout << "Picked Numbers: ";
    for (int number : pickedNumbers)
        std::cout << number << " ";
    std::cout << std::endl;
    
}