I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> I should be using for linked lists. Should it be <list>? Thanks.
Printable View
I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> I should be using for linked lists. Should it be <list>? Thanks.
It should be <list>.
When a list is instantiated, is it granted that the list would start out with no node?
if you construct it like this:
then, yes, there are no nodes.Code:list<int> m_list;
The list is empty, its size is 0, and there's no data in it. Interestingly enough, that doesn't mean there are no nodes.
Thanks for your replies. I'm having a complaint when I'm instantiating my lists as described in my code of a class definition below:
The compiler (Microsoft Visual C++ version 6.0) doesn't seem to link the red lines, even though that the types is a struct type. Do you see anything wrong with the struct types that I've defined?Code:#include <list>
struct Item
{
int PartNumber;
int Quantity;
int Cost;
};
struct LabourItem
{
int LabourType;
int Cost;
};
class JobList
{
private:
list<Item> PartOrders;
list<LabourItem> Labour;
public:
JobList(); // Default Constructor
void AddItem(int PartNumber, int Quantity, int Cost);
void AddLabourItem(int Labour, int Cost);
~JobList(); // Destructor
};
Magic might happen if you say "std::list" instead.
Thanks. I probably forgotten about "using namespace std;"
I have another question regarding adding contents in the stl linked lists. For my class definition below:
Let's say that I want to implement the AddItem function:Code:#include <string>
#include <vector>
#include <exception>
#include <list>
using namespace std;
#ifndef __Job_List_h__
#define __Job_List_h__
#include "Receptionist.h"
#include "Technician.h"
namespace Main_Class_Diagram
{
class Receptionist;
class Technician;
class Job_List;
}
namespace Main_Class_Diagram
{
struct Item
{
int PartNumber;
int Quantity;
int Cost;
};
struct LabourItem
{
int LabourType;
int Cost;
};
}
namespace Main_Class_Diagram
{
class Job_List
{
private: Main_Class_Diagram::Receptionist* _unnamed_Receptionist_;
private: Main_Class_Diagram::Technician* _unnamed_Technician_;
private: list <Item> PartOrders;
list <LabourItem> LabourOrders;
public: void AddItem(int PartNumber, int Quantity, int Cost);
public: void AddLabourItem(int Labour, int Cost);
public: void PrintInvoice();
void NewInvoice();
};
}
#endif
For some reason, I can't access the highlights in my IDE to the struct components properly. Does anyone know whether I'm accessing the struct components of PartOrders properly? How would I store those values into the struct contents properly?Code:#include <iostream>
#include <string>
#include <vector>
#include <exception>
#include <list>
using namespace std;
#include "Job_List.h"
#include "Receptionist.h"
#include "Technician.h"
void Main_Class_Diagram::Job_List::AddItem(int PartNumber, int Quantity, int Cost) {
PartOrders.push_back();
PartOrders.PartNumber = PartNumber;
PartOrders.Cost = Cost;
PartOrders.Quantity = Quantity;
}
You can't push_back ... nothing. Create an Item, set its fields, and push_back that Item.
Edit: IOW, the list doesn't have any fields, which is why you can't access them. Items have fields.
That brings me another question: If I want to access any one of the entries, do I have to store the Item into a temporary Item somehow?
No, you should use an iterator to access elements of the list.
By the way, each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. As such, instead of using __Job_List_h__ as your header inclusion guard name, use say, JOB_LIST_H_.