-
STL lists
I'm wondering if I can create a list inside a list inside STL and how I would do that? So it is sort of a 2d linked list.
Lets say I have two classes, bookTypes and books... I want to create a list of bookTypes like history, mystery, etc. And then add the list containing names of books belonging to each type.
I have this for the startup:
std::list<bookTypes> types;
typedef std::list<bookTypes>::iterator typeIt;
bookTypes myNewBookType;
myNewBookType.add(typeOfBook); //some add function from the bookTypes class
types.pushback(myNewBookType);
for (typeIt=types.begin(); typeIt!=types.end(); ++typeIt)
{
// NOW HOW DO I ADD THE NAME OF THE BOOK IN HERE FOR EACH BOOK TYPE
//thinking of doing stuff to *typeIt but not sure how
}
also just for confirmation, this adds new items to the front of the list, right? So when I do pushback all the prior items will be pushed back and last added item will be popped first right?
Thanks!
-
Of course you can use a list as the element type of another list:
Code:
typedef list<int> IntList;
typedef IntList::iterator IntListIter;
typedef list<IntList> ListList;
typedef ListList::iterator ListListIter;
/// ...
ListList ll;
// resize it/initialise it....
for(ListListIter listIt = ll.begin(); listIt != ll.end(); listIt++)
{
for(IntListIter intIt = listIt->begin(); intIt != listIt->end(); intIt++)
{
*intIt = someIntReturningFunction();
}
}
Cheers,
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
well, in that *intIt = someIntReturningFunction(); is that function part of IntList or ListList? I'm trying to access lets say a feed function that is part of the nested list (ListList).
-
Also i wanted to ask, let say I wanna do something at a particular location... can I do something like:
for (intIt = listIt->begin(); intIt != listIt->end(); intIt++)
{
if(intIt == 3)
{
// do something here
}
}
-
someIntReturningFunction() is just a place holder because I didn't want to suggest you have to insert a particular int value.
you could write
if you like, which sets all values in the list to the same value 4711.
If you want random access to the elements, you should consider using a different STL container (vector/map) for perfomance issues, but you can still address list elements by random access. Just be aware, that the list is iterated from the beginning.
Code:
list<int> iList;
// fill the list
for(int i= 0; i < 10000000; i++)
iList.push_back(i*i);
iList[898889] = 0; // uses the std::list + - operator 898889 times starting from iList.begin(): NIGHTMARE!!!
think about which container you use for which purpose.
vector is ideal when your index type is int and you never need to delete or insert elements somewhere in the middle and you need random access to the elements
map is a good choice when you need to delete and insert frequently and still need random access to elements
list is good when you only want to access elements in order (not random access) and insert/delete at your current position
These are just guidelines, so think about the problem at hand and how you want to use your structures.
Cheers,
D
-
If you need random access to a container's elements (e.g., the third element), you should probably use vector, not list
Danny Kalev
-
I probably understand what is going here.
The problem is 2d List
booktypes book book book
C++ 1b 2b 3b
C 1b 1b 1b
Do you mean like this ?
I wonder why u sing typedef because it is very confusing.
I seldom used it since class also make the user defined type.
Please correct me if i wrong.
-
No, I meant something like:
X x;
x[3]= 5;
int z= x[4];
etc.
Danny Kalev
Similar Threads
-
Replies: 4
Last Post: 01-08-2007, 04:50 PM
-
Replies: 0
Last Post: 09-19-2005, 07:21 AM
-
Replies: 0
Last Post: 01-11-2002, 04:46 AM
-
By Jamie Harsevoort in forum Enterprise
Replies: 0
Last Post: 11-30-2000, 04:34 PM
-
By Shiv Shankar Ramakrishnan in forum .NET
Replies: 29
Last Post: 09-01-2000, 03:52 PM
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks