Thought I’d share a useful piece of code I wrote the other day. I’m sure there already exists something that does this, but it’s good to have your own version too.

Usage is pretty simple:

CContainer<int> myContainer;
myContainer.init(10);
myContainer.insertIntoContainer(5,0);
myContainer.insertIntoContainer(10,1);

cout << "First slot in the container is " << myContainer.getItemAt(0) << ".\nSecond slot in the container is " << myContainer.getItemAt(1) << "\n";

if(myContainer.isFree(3)){
    // Slot 3 in the container is free!
}
if(myContainer.isFree(0)){
    // Slot 0 in the container is not free!
}

 

Think that should be pretty self explanatory. Grab the source file, CContainer.h here.

There certainty is room for improvement (Like better names for the member functions), but I’ll leave it as it is for now.

October 24, 2011 Posted by | Main Page, Programming | Comments Off

Sorry, the comment form is closed at this time.