|
-
June 27th, 2002, 05:03 PM
#1
Need help with LinkList
Got a class in which I need a double LinkList (X). This DLList X has another DoubleLinkList (Y) within its nodes.
Now I'm using STL List.h template. The issue I'm facing maybe very simple for you but a little confusing for me..... Hoping that someone can help me with it.
class Paul
{
public: Paul(); //Construction
~Paul(); //Destructor
CreateList();
private:
typedef struct XNode
{
long country;
list <CString> csCities;
};
list <XNode> XList,
}
Question:
a) how will I initialise this XList?
b) If I want to pass this xList as a ref variable in a funtion, how will that be done?
c) how will I create a new XNode? Basically, anything especial I have to do for csCities list creation?
c) How will the destructor work?
Thanks for your kind perusal.
-
June 27th, 2002, 05:47 PM
#2
Basically, it all just works. My guess is that your main confusion has to do with notation. What I always do is typedef the templated items as follows:
typedef std::list<CString> MyCStringList;
typedef MyCStringList::iterator MyCStringListIter;
typedef MyCStringList::const_iterator MyCStringListConstIter;
This way, you don't need to use the template parameter when accessing the class.
a) how will I initialise this XList?
It's initialized as soon as the class is initialized.
b) If I want to pass this xList as a ref variable in a funtion, how will that be done?
Simply pass it. The argument type would be CXNodeList& if you typedef as I described above.
c) how will I create a new XNode? Basically, anything especial I have to do for csCities list creation?
Nothing special needed.
c) How will the destructor work?
It will work just fine.
Jeff
-
June 28th, 2002, 04:21 PM
#3
Thanks Jeff,
Ur typedef has made it simpler to see the structure and has helped me. You are also right in mentioning that I'm getting confused with the notations.
Couple of questions though....
a) when I'm creating a new XNode for the XList, how would I create the csCities linkedlist?
typedef std::list<XNode> MyNodeList
MyNodeList nList;
XNode xTemp;
xTemp.country = 12;
xTemp.csCities->list(); //error for creating list here
nList.push_front(xTemp);
b) How does iteration work? How would I traverse the node and check for country?
I know it would be simple but I'm lost... Is there any place from where I can reference examples for using this STD::list?
thanks for everyones time for coming here and seeing the possibilty of helping. Especial thanks to Jeff for taking the time to actually answering my questions.
-
June 28th, 2002, 04:35 PM
#4
The csCities linked list is created when you createteh XNode, although it will of course be empty.
You add items as follows:
xTemp.csCities.push_back("Chicago");
to iterate through the list, you would do:
Code:
for( MyCStringListConstIter iter = xTemp.csCities.begin(); iter != xTemp.csCities.end(); ++iter )
{
CString cityName = *iter;
...
}
I understand your confusion. It is somewhat difficult to learn. MSDN documentation is difficult and the source code is worse. Somebody else would be better at recommending a book--I started using STL on an SGI computer when there were no decent books available, at least none that I found. It was rather frustrating.
Stroustrup's book is good as a reference, but not necessarily to learn from.
Anybody have a book recommendation for an STL book?
Jeff
-
June 28th, 2002, 04:37 PM
#5
The C++ Standard Library -- Josuttis
-
June 28th, 2002, 04:38 PM
#6
Matthew Austern's book is good as a reference, and Paul MacKenzie recommends Josuttis's book (I've not read it). Definitely read "Effective STL" by Scott Meyers.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 28th, 2002, 05:05 PM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|