Misunderstanding in use of class variables from a header file - c++

struct TodoItem
{
std::string todo;
};
const int MAX_STACK_SIZE = 5;
class TodoStackArray
{
public:
TodoStackArray(); //confusion here<---
bool isEmpty();
bool isFull();
void push(std::string todoItem);
void pop();
TodoItem* peek();
/* for grading purposes we need these following methods */
int getStackTop() { return stackTop; }
TodoItem** getStack() { return stack; }
private:
int stackTop;
TodoItem* stack[MAX_STACK_SIZE];
};
Above, is some declarations and a class from a header file for a current assignment. Our duty was to implement a stack program using this header file. However, I am a little confused as to the purpose of the TodoStackArray() in the Header File. Is this supposed to be a base constructor? Am I supposed to use it for anything??
I understand that this is somewhat situation specific but given that stack Arrays are STL I figured you might all be able to provide some insight. Thanks!
In case you want to see what I did with this header file...
TodoStackArray::TodoStackArray() //This, unsurprisingly, produces an error.
{
stackTop = -1;
stack[stackTop];
}
bool TodoStackArray::isEmpty()
{
return (stackTop == -1);
}
TodoItem* TodoStackArray::peek()
{
if(stackTop ==-1)
{
cout<< "Stack empty, cannot peak."<<endl;
}
else
{
return(stack[stackTop]);
}
}
bool TodoStackArray::isFull()
{
return(stackTop == 4);
}
void TodoStackArray::push(std::string todoItem)
{
if(stackTop >= 5)
{
cout<<"Stack full, cannot add new todo item."<<endl;
}
else
{
stack[stackTop++];
stack[stackTop]->todo = todoItem;
}
}
void TodoStackArray::pop()
{
if(stackTop == -1)
{
cout<<"Stack empty, cannot pop an item."<<endl;
}
else
{
stackTop--;
}
}
Also, to be clear, we were not provided driver software. They will be assessing from their own stuff so we have to write our own driver software to test our functions. Hence the lack of any Main implementation.

Yes, TodoStackArray::TodoStackArray() really is your class' default constructor.
What you are supposed to do with it just as with any default constructor:
How are the class members supposed to be initialized if I create a new TodoStackArray?
The answer depends on what your class is supposed to look like in its initial state.
In you situation, "by default" a stack is empty. Setting the stackTop to -1 was not a bad idea and the rest of your implementation is consistent with that (stackTop == -1 clearly means "empty stack").
The odd thing that happens in your constructor is this:
stack[stackTop];
What is, according to you, the purpose of this line? You are reading the value in your array at a negative index, and we all know this kind of things never end well.
Constructors are made to initialize your data. Here you are reading something you didn't initialize at an index that doesn't exist. Doesn't make much sense, does it? :)
Just get rid of this line and you should be able to move on. Here is an equivalent implementation using member initializer list (which are considered nicer):
TodoStackArray::TodoStackArray() : stackTop(-1)
{
// notice: no body required here
}
Have fun!

Related

Deleting an element in a vector by value

How can I retrieve an object from the Flight to be compared to the input (flightNumber) in the main? How do I declare the attributes type in the main?
When I compile, a error message is displayed: invalid conversion of 'int' to '*Flight*' at agent1.delete(flightNumber);.
class Flight
{
int FlightNumber
};
class TravelAgent
{
vector <Flight *> flightList;
};
void Agent::delete(Flight *obj)
{
vector<Flight*>::iterator ptr;
for(ptr=flightList.begin();ptr!=flightList.end();ptr++)
{
if((*Ptr)==obj)
{
flightList.erase(ptr);
break;
}
}
if ((ptr) == flightList.end())
{
cout<<"Flight not found"<<endl;
}
}
int main
{
Agent agent1;
int flightNumber;
cout<<"Enter the number of the flight: "<<flush;
in>>flightNumber;
agent1.delete(flightNumber);
}
You can add(if not present) a getter in Flight class
class Flight{
int FlightNumber;
public:
int getflightNumber(){ return flightNumber;}
};
and go as following:-
void Agent::delete(int flightNumber)
{
vector<Flight*>::iterator ptr;
for(ptr=flightList.begin();ptr!=flightList.end();ptr++)
{
if(((*Ptr)->getflightNumber() == flightNumber)
{
flightList.erase(ptr);
break;
}
}
if ((fPtr) == listFlight.end())
{
cout<<"Flight not found"<<endl;
}
}
Since the code here isn't fully functional, it's hard to give you good advice.
First, your error happens because you call (what seems to be) the member function, void Agent::delete(Flight *obj), with a variable of type int instead of type Flight. The compiler is not able to interpret your Flight object as an int, so it throws an error.
Secondly, you want to know how to retrieve attributes from an object. I will advise you to have a look to accessors and mutators.
If you want to retrieve information hold in your Flight object, you should expose member functions allowing that.
// in your header file
class Flight
{
private:
int flight_number;
public:
// retrieve flight number value
int get_flight_number(void) const;
// allow to set the flight number value
void set_flight_number(int new_flight_number);
// some other member functions
}
// in your source file
int Flight::get_flight_number(void) const
{
return this->flight_number;
}
void Flight::set_flight_number(int new_flight_number)
{
// let's do some verification (do whatever you want)
if (new_flight_number > 0)
{
this->flight_number = new_flight_number;
}
}
This way you will be able to set and access your flight_number by writing, for example :
void test_function(Flight *f)
{
if (f->get_flight_number() == 42)
{
// do some stuff
}
}
int main()
{
Flight *my_f = new Flight();
my_f->set_flight_number = 4242;
my_test_function(my_f);
}
Now, you have enough information to get going.
NOTES :
You heavily use pointers. Modern C++ strongly tends to not! Try to use references or move operation. You can consult this pages for info:
cpp-reference - references
cpp-reference - move semantics
It's a bit hardcore for beginner though. The web is full of great article. about it
You original error is in your main method. You need to change it so that instead of passing the flight number to your delete method, you create an instance of your Flight class.
int main() { // you are also missing parenthesis
Agent agent1;
int flightNumber;
cout<<"Enter the number of the flight: "<<flush; // I don't know what flush is but maybe you meant std::endl
cin>>flightNumber;
Flight flight(flightNumber);
agent1.delete(&flight); // delete takes a Flight* not an int
}
This requires that your Flight class have an appropriate constructor.
class Flight
{
public:
Flight(int flightNumber)
: flightNumber_(flightNumber)
{}
private:
int flightNumber_;
};
Then in your delete method you search your vector for the Flight instance that has the same flightNumber_ as the Flight you want to remove from your vector. This will require your Flight class to have some way of returning it's flightNumber_ member variable.
This is definitely NOT the best way to do this and is far from being in accordance with modern C++ standards but it should get you going.

Position in Member Declaration Breaks Code?

A while ago I asked a question on why the following code did not work:
std::vector<std::vector<std::vector<Tile_Base*>>> map_tile; // This is located in Map object. See below.
int t_x, t_y;
t_x = t_y = 200;
map_tiles.begin(); // clear(), resize() and every other function still causes problems
The thing is, is that it should have worked, yet Visual Studios 2012 throws an exception when the resize function is called. The exception pointed to this piece of code:
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
located in xutility. It said that there was an violating on access to reading the memory. I thought maybe somehow I lost access to the member along the way? (Using VS' watch I saw the memory was not corrupted)
So, I fiddled around with the code and tried to figure out what could possibly be going wrong, and after awhile I moved the map_tiles object down to the bottom of the list, and it worked:
// WORKS
class Map {
std::vector<Tile_Base*> spawn_tiles;
// map tile specific
bool Is_Valid(int,int);
std::string name;
std::vector<std::vector<std::vector<Tile_Base*> > > map_tiles;
public:
// ...
}
// DOESN'T WORK
class Map {
std::vector<std::vector<std::vector<Tile_Base*> > > map_tiles;
std::vector<Tile_Base*> spawn_tiles;
// map tile specific
bool Is_Valid(int,int);
std::string name;
public:
// ...
}
Any help pointing out what went wrong? I can't come up with any reasonable explanation.
A vector<T> comprises two discrete sets of data: the internal state and the array of Ts. The internal state - capacity, size, pointer - is separate from the array. The issue you're describing is normally caused by something overwriting the vector object, i.e the internal state. To track this down easily you could use a container class:
typedef std::vector<std::vector<std::vector<Tile_Base*> > > maptiles_t;
class CMapTiles
{
unsigned int m_guard;
maptiles_t m_tiles;
enum { Guard = 0xdeadbeef };
public:
CMapTiles() : m_guard(Guard), m_tiles() {}
~CMapTiles() { assert(m_guard == Guard); }
void Check()
{
#if defined(DEBUG)
if (m_guard != Guard)
DebugBreak();
#endif
}
void Resize(size_t x, size_t y)
{
Check();
auto init = std::vector<std::vector<Tile_Base*> >(y/32);
m_tiles.resize(m_x / 32, init);
Check();
}
const maptiles_t& tiles() const { Check(); return m_tiles; }
maptiles_t& tiles() { Check(); return m_tiles; }
};
And instead of using std::vector<...> map_tiles have CMapTiles map_tiles, and then when you want to get at the vector, map_tiles.tiles().
Hope this helps.

C++ std::vector::clear() crash

I've got a program where I have a std::vector as a member of a class:
class Blackboard
{
public:
inline std::vector<Vector2<int> > GetPath()
{ return m_path; }
inline void SetPath(std::vector<Vector2<int> > path)
{ m_path = path; }
inline void ClearPath()
{ if(m_path.size() > 0) m_path.clear(); }
private:
std::vector<Vector2<int>> m_path;
};
Where the Vector2 class is defined as:
template <class T>
class Vector2
{
private:
T m_x;
T m_y;
public:
Vector2(void)
{ m_x = 0; m_y = 0;}
Vector2(T x, T y)
{ m_x = x; m_y = y;}
~Vector2(void)
{ }
inline T x() const
{ return m_x; }
inline T y() const
{ return m_y; }
// ...
};
And at some point I call:
m_blackboard.ClearPath();
This works fine in debug, but crashes in release with the "Microsoft Visual Studio C Runtime Library has detected a fatal error in Test2.exe." message.
The call stack, at the last point where I can still see shows that:
Test2.exe!std::vector<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >::erase
(std::_Vector_const_iterator<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >
_First_arg={m_x=15 m_y=7 },
std::_Vector_const_iterator<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >
_Last_arg={m_x=15 m_y=8 }) Line 1037 + 0xe bytes C++
Here is where I'm calling the code that ends up crashing:
BTNode::Status GoToDestBehavior::Update()
{
BTEntityData::Node* node = m_dataRef->m_bTree.GetNode(m_index);
if(node->m_state == BTNode::STATE_READY)
{
BehaviorTree::RequestDeferredAction(Batch::PATHFIND, m_dataRef->m_entityID);
return BTNode::STATE_RUNNING;
}
else if(node->m_state == BTNode::STATE_RUNNING)
{
std::vector<Vector2<int>> path = m_dataRef->m_blackboard.GetPath();
EntitySystem::Entity* entity = EntitySystem::GetEntity(m_dataRef->m_entityID);
Assert(entity != NULL, "Invalid entity\n");
Assert(entity->HasComponent(Component::PHYSICS_COMP), "Associated entity must have physics component to move\n");
int phyIndex = entity->GetComponentIndex(Component::PHYSICS_COMP);
PhysicsSystem::PhysicsData * physicsData = PhysicsSystem::GetComponent(phyIndex);
Assert(physicsData != NULL, "Invalid physics data\n");
// Path is empty, so finish
if(path.size() == 0)
{
physicsData->m_dir = Direction::NONE; // Stop because we are here
return BTNode::STATE_SUCCESS;
}
// Remove last element if we are at it
//LogFmt("Size of vector %d\n", path.size());
Vector2<int> last = path.back();
if(last.x() == physicsData->m_posX && last.y() == physicsData->m_posY)
{
path.pop_back();
}
// Last node of the path has been transversed
if(path.size() == 0)
{
physicsData->m_dir = Direction::NONE; // Stop because we are here
m_dataRef->m_blackboard.ClearPath();
return BTNode::STATE_SUCCESS;
}
Vector2<int> step = path.back();
physicsData->m_dir = Direction::VectorToDirection(physicsData->m_posX, physicsData->m_posY, step.x(), step.y());
if(physicsData->m_dir == Direction::NONE)
{
m_dataRef->m_blackboard.SetPath(path);
return BTNode::STATE_FAIL;
}
m_dataRef->m_blackboard.SetPath(path);
return BTNode::STATE_RUNNING;
}
return BTNode::STATE_ERROR;
}
I don't know why it's behaving like this. Most similar issues I've found online have the problem of calling clear on an empty array, but I have a guard against that, so it shouldn't be the issue.
The other thing I can think of is my Vector2 class requiring some kind of copy constructor or something for when I add elements to the vector, but in the end it's just 2 ints, so I don't know why that might be failing.
I've been over this code too much and might be missing something obvious.
It's perfectly fine to call clear on an empty container of any sort.
Using my psychic debugging skills, I have determined that in code you aren't showing us you're accessing elements of the vector that don't actually exist (possibly before you inserted them, and probably with operator[]). Usually element creation is done through resize, push_back, or insert.
The other possibility is that you have another memory corruption somewhere in your program.
I found an issue I had due to a change in data format. The std::list I was using changed from a pointer to a list to directly the list. This started causing all sorts of errors that checking for the size of the list did not solve and were caused by a ZeroMemory()/memset() call that wiped out all of the tracking data of the list, since it was now part of the class instead of a pointer to the list.
If you have an empty list and call .clear() on it with a crash, chances are you have messed up the internal tracking memory as mentioned by Mark in his answer. Look for a place where you are doing memory clearing on containing classes and the like as the most likely culprits.
I know it's been 8 years, but I thought too I had this problem when I was destroying an empty bst into which my code was sending a nullptr value to the __p variable in the implementation of "new_allocator.h". This __p is needed to never be null, as mentioned in the file itself!
// __p is not permitted to be a null pointer.
The solution is not sending anything if you don't have something to send, basically.

What's wrong with this class?

I think the problem is in main() but this compiles fine but I get no output. I think maybe it's not initalizing correctly because in debug mode it says
"myCharQ {item=0x0018fa00 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̺yâpú" front=-858993460 rear=-858993460 ...}"
How would you rewrite this so that it is proper? I'm just starting out with classes so any help would be useful.
The following is a Array based Queue class
#include <iostream>
#include <cstdlib>
using namespace std;
const int MaxQueueSize = 10; // Queue Struct can hold up to 10 char.
typedef char ItemType; // the queue's data type is char
class CPPQueue
{
public:
CPPQueue();
ItemType item[MaxQueueSize];
void initQueue(CPPQueue q);
bool IsEmpty(CPPQueue q);
bool IsFull(CPPQueue q);
void Enqueue(CPPQueue q, ItemType newItem);
void PrintQ(const CPPQueue q);
void PrintQueueInfo(CPPQueue myQ);
ItemType Dequeue(CPPQueue q);
private:
int front, rear;
int count;
};
CPPQueue::CPPQueue()
{
int front, rear, count = 0;
}
void CPPQueue::initQueue(CPPQueue q)
{
q.front = q.rear = q.count = 0;
}
bool CPPQueue::IsEmpty(CPPQueue q)
{
return (q.count == 0);
}
bool CPPQueue::IsFull(CPPQueue q)
{
return (q.count == MaxQueueSize);
}
void CPPQueue::Enqueue(CPPQueue q, ItemType newItem)
{
if(q.count == MaxQueueSize)
{
cerr << "Error! Queue is full, cannot enqueue item.\n" << endl;
exit(1);
}
q.item[q.rear] = newItem;
q.rear++;
if (q.rear == MaxQueueSize)
{
q.rear = 0; // adjustment for circular queue
}
q.count++;
}
ItemType CPPQueue::Dequeue(CPPQueue q)
{
ItemType theItem;
if(q.count == 0)
{
cerr << "Error! Queue is empty, cannot dequeue item.\n" << endl;
exit(1);
}
theItem = q.item[ q.front ];
q.front++;
if (q.front == MaxQueueSize)
{
q.front = 0; // adjustment for circular queue
}
q.count--;
return theItem;
}
// Function PrintQ() prints the contents of the queue without changing
// the queue. Printing starts at the "front" index and stops before we
// get to the "rear" index. A decrementing counter controls the loop.
//
void CPPQueue::PrintQ(const CPPQueue q)
{
int i;
int qindex = q.front;
for(i = q.count; i > 0; i--)
{
cout << q.item[qindex] ;
qindex = (++qindex) % MaxQueueSize; // adjustment for circular queue
if(i > 1)
cout << ", ";
}
}
// Helper function for the main program below.
void CPPQueue::PrintQueueInfo(CPPQueue myQ)
{
cout << "The queue contains: ";
PrintQ(myQ);
cout << endl;
}
int main()
{
CPPQueue myCharQ;// queue holds characters
char ch; // char dequeued
myCharQ.initQueue(myCharQ);
myCharQ.Enqueue(myCharQ, 'a'); myCharQ.PrintQueueInfo(myCharQ);
myCharQ.Enqueue(myCharQ, 'b'); myCharQ.PrintQueueInfo(myCharQ);
myCharQ.Enqueue(myCharQ, 'c'); myCharQ.PrintQueueInfo(myCharQ);
ch = myCharQ.Dequeue(myCharQ); myCharQ.PrintQueueInfo(myCharQ);
ch = myCharQ.Dequeue(myCharQ); myCharQ.PrintQueueInfo(myCharQ);
myCharQ.Enqueue(myCharQ, 'e');
myCharQ.Enqueue(myCharQ, 'f'); myCharQ.PrintQueueInfo(myCharQ);
myCharQ.Enqueue(myCharQ, 'g'); myCharQ.PrintQueueInfo(myCharQ);
cout << endl;
// print the dequeued characters
while(!myCharQ.IsEmpty(myCharQ))
{
ch = myCharQ.Dequeue(myCharQ);
cout << ch << " ";
}
cout << endl << endl;
return 0;
}
You never initialize the member variables front, rear, and count. You shadow them in your constructor by declaring variables with the same names again. Drop the int and just assign them (though this is not why the values aren't printed correctly, more on that in a bit). Actually, don't do that either; use an initializer list:
CPPQueue::CPPQueue()
: front(0), rear(0), count(0)
{ }
Also, why do you have an initQueue function? You already have a constructor, rely on that to initialize your instance(s) (this is not C!).
Next, functions like IsEmpty are non-static member functions, yet they don't operate on the current instance. Don't take a queue as a parameter, just return if the instance is empty, full, whatever. Your code would have to be used like this:
Queue q;
q.IsEmpty(q);
Just strange. All of your member functions operate this way. When you cann a member function an implicit pointer to the current instance is passed as a hidden parameter (this). Therefore, each time the function is called it operates within the context of the instance it was called upon. You don't need to take an instance as a parameter.
Also realize that all of your functions take their arguments by value. You are going to be creating copies of these queues like crazy. If you modify the argument it will not be seen by the caller. For example:
void CPPQueue::initQueue(CPPQueue q)
{
q.front = q.rear = q.count = 0;
}
That is essentially useless (aside from that fact that an initialize function is unnecessary). The changes to q.front, q.rear, and q.count will not be visible outside of that function as you are operating on a copy.
So even though your constructor is broken due to variable shadowing, this is why you still don't print what you expect to after calling initQueue. You are modifying a copy.
As for your implementation, it is not robust at all. You expose the underlying array to clients of your class. This is a bad idea. The items in a queue should not be directly accessible. What if I decide to muck with the array directly? Now all of your state variables are wrong. front, rear, and count are all potentially invalid as I have modified the state of the queue without going through any of your functions.
It's not even necessary; all I should be able to do is queue and dequeue items. That's it. That's what a queue does. It is not an array, if I want an array I will use one.
So, in summary, kudos on beginning to learn a relatively complex language. Keep at it and don't get discouraged, we all have to learn this stuff at some point.
EDIT: I have to run, but here is a quick rewrite of some of your class. I have removed your typedef for the item type. Why? It is unnecessary. You are not going to change it to another type per some platform or other environmental change, so the typedef only hurts the usability of your class. typedefs are good for things that may change (i.e., int32_t) for some environmental reason, but if they aren't helping you or clients of your code they are just one more thing to get in the way.
class CPPQueue
{
public:
CPPQueue();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(char newItem);
char Dequeue();
void PrintQ() const;
void PrintQueueInfo() const;
private:
char item[MaxQueueSize];
int front
int rear;
int count;
};
CPPQueue::CPPQueue()
: front(0), rear(0), count(0) { }
bool CPPQueue::IsEmpty() const
{
// you don't actually need the this pointer
// here, but I included it to make it clear
// that you are accessing the count variable
// for the current instance of a CPPQueue
return this->count == 0;
}
I hope this helps you rewrite the rest of your class, gotta go now. I added const in teh declaration of functions that should not mutate the internal state of a CPPQueue. Do a search for "const correctness" to get a better idea of why you would do such a thing. Good luck!
In your constructor:
int front, rear, count = 0;
is wrong. These are local variables that shadow your member variables.
You should use member initializers (with colon after your constructor name) instead.
Also note that you are passing by value all over the place - you probably want to pass by reference instead - look at each function parameter and ask yourself, "do I want a new copy of my parameter or do I want to refer to the same parameter (same memory location) that I passed in?"
CPPQueue::CPPQueue() :
front(0), rear(0), count(0)
{
}
Note: #OP this is elementary-level C++ - you need to read up and get your basics down or you will run into many, many much more difficult to fix problems further down the line.

Bin packing implementation in C++ with STL

This is my first time using this site so sorry for any bad formatting or weird formulations, I'll try my best to conform to the rules on this site but I might do some misstakes in the beginning.
I'm right now working on an implementation of some different bin packing algorithms in C++ using the STL containers. In the current code I still have some logical faults that needs to be fixed but this question is more about the structure of the program. I would wan't some second opinion on how you should structure the program to minimize the number of logical faults and make it as easy to read as possible. In it's current state I just feel that this isn't the best way to do it but I don't really see any other way to write my code right now.
The problem is a dynamic online bin packing problem. It is dynamic in the sense that items have an arbitrary time before they will leave the bin they've been assigned to.
In short my questions are:
How would the structure of a Bin packing algorithm look in C++?
Is STL containers a good tool to make the implementation be able to handle inputs of arbitrary lenght?
How should I handle the containers in a good, easy to read and implement way?
Some thoughts about my own code:
Using classes to make a good distinction between handling the list of the different bins and the list of items in those bins.
Getting the implementation as effective as possible.
Being easy to run with a lot of different data lengths and files for benchmarking.
#include <iostream>
#include <fstream>
#include <list>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct type_item {
int size;
int life;
bool operator < (const type_item& input)
{
return size < input.size;
}
};
class Class_bin {
double load;
list<type_item> contents;
list<type_item>::iterator i;
public:
Class_bin ();
bool operator < (Class_bin);
bool full (type_item);
void push_bin (type_item);
double check_load ();
void check_dead ();
void print_bin ();
};
Class_bin::Class_bin () {
load=0.0;
}
bool Class_bin::operator < (Class_bin input){
return load < input.load;
}
bool Class_bin::full (type_item input) {
if (load+(1.0/(double) input.size)>1) {
return false;
}
else {
return true;
}
}
void Class_bin::push_bin (type_item input) {
int sum=0;
contents.push_back(input);
for (i=contents.begin(); i!=contents.end(); ++i) {
sum+=i->size;
}
load+=1.0/(double) sum;
}
double Class_bin::check_load () {
return load;
}
void Class_bin::check_dead () {
for (i=contents.begin(); i!=contents.end(); ++i) {
i->life--;
if (i->life==0) {
contents.erase(i);
}
}
}
void Class_bin::print_bin () {
for (i=contents.begin (); i!=contents.end (); ++i) {
cout << i->size << " ";
}
}
class Class_list_of_bins {
list<Class_bin> list_of_bins;
list<Class_bin>::iterator i;
public:
void push_list (type_item);
void sort_list ();
void check_dead ();
void print_list ();
private:
Class_bin new_bin (type_item);
bool comparator (type_item, type_item);
};
Class_bin Class_list_of_bins::new_bin (type_item input) {
Class_bin temp;
temp.push_bin (input);
return temp;
}
void Class_list_of_bins::push_list (type_item input) {
if (list_of_bins.empty ()) {
list_of_bins.push_front (new_bin(input));
return;
}
for (i=list_of_bins.begin (); i!=list_of_bins.end (); ++i) {
if (!i->full (input)) {
i->push_bin (input);
return;
}
}
list_of_bins.push_front (new_bin(input));
}
void Class_list_of_bins::sort_list () {
list_of_bins.sort();
}
void Class_list_of_bins::check_dead () {
for (i=list_of_bins.begin (); i !=list_of_bins.end (); ++i) {
i->check_dead ();
}
}
void Class_list_of_bins::print_list () {
for (i=list_of_bins.begin (); i!=list_of_bins.end (); ++i) {
i->print_bin ();
cout << "\n";
}
}
int main () {
int i, number_of_items;
type_item buffer;
Class_list_of_bins bins;
queue<type_item> input;
string filename;
fstream file;
cout << "Input file name: ";
cin >> filename;
cout << endl;
file.open (filename.c_str(), ios::in);
file >> number_of_items;
for (i=0; i<number_of_items; ++i) {
file >> buffer.size;
file >> buffer.life;
input.push (buffer);
}
file.close ();
while (!input.empty ()) {
buffer=input.front ();
input.pop ();
bins.push_list (buffer);
}
bins.print_list ();
return 0;
}
Note that this is just a snapshot of my code and is not yet running properly
Don't wan't to clutter this with unrelated chatter just want to thank the people who contributed, I will review my code and hopefully be able to structure my programming a bit better
How would the structure of a Bin packing algorithm look in C++?
Well, ideally you would have several bin-packing algorithms, separated into different functions, which differ only by the logic of the algorithm. That algorithm should be largely independent from the representation of your data, so you can change your algorithm with only a single function call.
You can look at what the STL Algorithms have in common. Mainly, they operate on iterators instead of containers, but as I detail below, I wouldn't suggest this for you initially. You should get a feel for what algorithms are available and leverage them in your implementation.
Is STL containers a good tool to make the implementation be able to handle inputs of arbitrary length?
It usually works like this: create a container, fill the container, apply an algorithm to the container.
Judging from the description of your requirements, that is how you'll use this, so I think it'll be fine. There's one important difference between your bin packing algorithm and most STL algorithms.
The STL algorithms are either non-modifying or are inserting elements to a destination. bin-packing, on the other hand, is "here's a list of bins, use them or add a new bin". It's not impossible to do this with iterators, but probably not worth the effort. I'd start by operating on the container, get a working program, back it up, then see if you can make it work for only iterators.
How should I handle the containers in a good, easy to read and implement way?
I'd take this approach, characterize your inputs and outputs:
Input: Collection of items, arbitrary length, arbitrary order.
Output: Collection of bins determined by algorithm. Each bin contains a collection of items.
Then I'd worry about "what does my algorithm need to do?"
Constantly check bins for "does this item fit?"
Your Class_bin is a good encapsulation of what is needed.
Avoid cluttering your code with unrelated stuff like "print()" - use non-member help functions.
type_item
struct type_item {
int size;
int life;
bool operator < (const type_item& input)
{
return size < input.size;
}
};
It's unclear what life (or death) is used for. I can't imagine that concept being relevant to implementing a bin-packing algorithm. Maybe it should be left out?
This is personal preference, but I don't like giving operator< to my objects. Objects are usually non-trivial and have many meanings of less-than. For example, one algorithm might want all the alive items sorted before the dead items. I typically wrap that in another struct for clarity:
struct type_item {
int size;
int life;
struct SizeIsLess {
// Note this becomes a function object, which makes it easy to use with
// STL algorithms.
bool operator() (const type_item& lhs, const type_item& rhs)
{
return lhs.size < rhs.size;
}
}
};
vector<type_item> items;
std::sort(items.begin, items.end(), type_item::SizeIsLess);
Class_bin
class Class_bin {
double load;
list<type_item> contents;
list<type_item>::iterator i;
public:
Class_bin ();
bool operator < (Class_bin);
bool full (type_item);
void push_bin (type_item);
double check_load ();
void check_dead ();
void print_bin ();
};
I would skip the Class_ prefix on all your types - it's just a bit excessive, and it should be clear from the code. (This is a variant of hungarian notation. Programmers tend to be hostile towards it.)
You should not have a class member i (the iterator). It's not part of class state. If you need it in all the members, that's ok, just redeclare it there. If it's too long to type, use a typedef.
It's difficult to quantify "bin1 is less than bin2", so I'd suggest removing the operator<.
bool full(type_item) is a little misleading. I'd probably use bool can_hold(type_item). To me, bool full() would return true if there is zero space remaining.
check_load() would seem more clearly named load().
Again, it's unclear what check_dead() is supposed to accomplish.
I think you can remove print_bin and write that as a non-member function, to keep your objects cleaner.
Some people on StackOverflow would shoot me, but I'd consider just making this a struct, and leaving load and item list public. It doesn't seem like you care much about encapsulation here (you're only need to create this object so you don't need do recalculate load each time).
Class_list_of_bins
class Class_list_of_bins {
list<Class_bin> list_of_bins;
list<Class_bin>::iterator i;
public:
void push_list (type_item);
void sort_list ();
void check_dead ();
void print_list ();
private:
Class_bin new_bin (type_item);
bool comparator (type_item, type_item);
};
I think you can do without this class entirely.
Conceptually, it represents a container, so just use an STL container. You can implement the methods as non-member functions. Note that sort_list can be replaced with std::sort.
comparator is too generic a name, it gives no indication of what it compares or why, so consider being more clear.
Overall Comments
Overall, I think the classes you've picked adequately model the space you're trying to represent, so you'll be fine.
I might structure my project like this:
struct bin {
double load; // sum of item sizes.
std::list<type_item> items;
bin() : load(0) { }
};
// Returns true if the bin can fit the item passed to the constructor.
struct bin_can_fit {
bin_can_fit(type_item &item) : item_(item) { }
bool operator()(const bin &b) {
return item_.size < b.free_space;
}
private:
type_item item_;
};
// ItemIter is an iterator over the items.
// BinOutputIter is an output iterator we can use to put bins.
template <ItemIter, BinOutputIter>
void bin_pack_first_fit(ItemIter curr, ItemIter end, BinOutputIter output_bins) {
std::vector<bin> bins; // Create a local bin container, to simplify life.
for (; curr != end; ++curr) {
// Use a helper predicate to check whether the bin can fit this item.
// This is untested, but just for an idea.
std::vector<bin>::iterator bin_it =
std::find_if(bins.begin(), bins.end(), bin_can_fit(*curr));
if (bin_it == bins.end()) {
// Did not find a bin with enough space, add a new bin.
bins.push_back(bin);
// push_back invalidates iterators, so reassign bin_it to the last item.
bin_it = std::advance(bins.begin(), bins.size() - 1);
}
// bin_it now points to the bin to put the item in.
bin_it->items.push_back(*curr);
bin_it->load += curr.size();
}
std::copy(bins.begin(), bins.end(), output_bins); // Apply our bins to the destination.
}
void main(int argc, char** argv) {
std::vector<type_item> items;
// ... fill items
std::vector<bin> bins;
bin_pack_first_fit(items.begin(), items.end(), std::back_inserter(bins));
}
Some thoughts:
Your names are kinda messed up in places.
You have a lot of parameters named input, thats just meaningless
I'd expect full() to check whether it is full, not whether it can fit something else
I don't think push_bin pushes a bin
check_dead modifies the object (I'd expect something named check_*, to just tell me something about the object)
Don't put things like Class and type in the names of classes and types.
class_list_of_bins seems to describe what's inside rather then what the object is.
push_list doesn't push a list
Don't append stuff like _list to every method in a list class, if its a list object, we already know its a list method
I'm confused given the parameters of life and load as to what you are doing. The bin packing problem I'm familiar with just has sizes. I'm guessing that overtime some of the objects are taken out of bins and thus go away?
Some further thoughts on your classes
Class_list_of_bins is exposing too much of itself to the outside world. Why would the outside world want to check_dead or sort_list? That's nobodies business but the object itself. The public method you should have on that class really should be something like
* Add an item to the collection of bins
* Print solution
* Step one timestep into the future
list<Class_bin>::iterator i;
Bad, bad, bad! Don't put member variables on your unless they are actually member states. You should define that iterator where it is used. If you want to save some typing add this: typedef list::iterator bin_iterator and then you use bin_iterator as the type instead.
EXPANDED ANSWER
Here is my psuedocode:
class Item
{
Item(Istream & input)
{
read input description of item
}
double size_needed() { return actual size required (out of 1) for this item)
bool alive() { return true if object is still alive}
void do_timestep() { decrement life }
void print() { print something }
}
class Bin
{
vector of Items
double remaining_space
bool can_add(Item item) { return true if we have enough space}
void add(Item item) {add item to vector of items, update remaining space}
void do_timestep() {call do_timestep() and all Items, remove all items which indicate they are dead, updating remaining_space as you go}
void print { print all the contents }
}
class BinCollection
{
void do_timestep { call do_timestep on all of the bins }
void add(item item) { find first bin for which can_add return true, then add it, create a new bin if neccessary }
void print() { print all the bins }
}
Some quick notes:
In your code, you converted the int size to a float repeatedly, that's not a good idea. In my design that is localized to one place
You'll note that the logic relating to a single item is now contained inside the item itself. Other objects only can see whats important to them, size_required and whether the object is still alive
I've not included anything about sorting stuff because I'm not clear what that is for in a first-fit algorithm.
This interview gives some great insight into the rationale behind the STL. This may give you some inspiration on how to implement your algorithms the STL-way.