|
-
May 14th, 2006, 10:32 AM
#1
List Class
Dears
I created a list class as
list<string> mylist
then added data
mylist.push_back("A");
mylist.push_back("B");
mylist.push_back("C-VIP");
mylist.push_back("D-VIP");
Now, I would like to create another list for keeping track of the
address of certain entries in the mylist class such as:
list<whatever??> myptr;
I would like myptr to have the addresses of the VIP entries above. Is
that possible?
How?
Thank You
HAOB BOY
-
May 14th, 2006, 03:33 PM
#2
Re: List Class
The list class is designed to keep you away from raw access to pointers (That's why iterators were invented). The other problem you face is that when a list is updated the iterators become invalidated, so you can't store them easily either. Perhaps there is a way around this limitation. What is the purpose of keeping track of the addresses (pointers?) of the VIP entries?
-
May 14th, 2006, 10:57 PM
#3
Re: List Class
may be the VIP example is not good but the purpose I want to keep track of certain nodes in a Linked List is that I am creating a memory-based database for a school project. There are several linked lists inmy project: one of them contains the table definitions which I will refer to whenever I need to add a record to the table linked list.
My probelm is how to know that a certain node is a definistion for certain table.
Thanx
-
May 14th, 2006, 11:19 PM
#4
Re: List Class
Have you thought of using sets or multisets?
-
May 14th, 2006, 11:46 PM
#5
Re: List Class
 Originally Posted by binarybob0001
The other problem you face is that when a list is updated the iterators become invalidated,
This is not necessarily true for lists. The iterator to an element remains valid for the lifetime of the list, unless the element is removed from the list.
Regards,
Paul McKenzie
-
May 14th, 2006, 11:50 PM
#6
Re: List Class
 Originally Posted by HAOBBOY
may be the VIP example is not good but the purpose I want to keep track of certain nodes in a Linked List
Then you need to keep track of the iterator to an element in the list, not pointer to an element in the list.
Basically when dealing with std::list, you cannot use pointers to address elements, only iterators. Yes, a list::iterator may be implemented as a pointer, but iterators and pointers are not the same thing.
Regards,
Paul McKenzie
-
May 15th, 2006, 06:52 AM
#7
Re: List Class
Thank you all
can I do this:
string d;
d="mylist";
list<string> d.c_str();
I would like to be able to create linked list at run time?
-
May 15th, 2006, 08:35 AM
#8
Re: List Class
 Originally Posted by HAOBBOY
Thank you all
can I do this:
string d;
d="mylist";
list<string> d.c_str();
I would like to be able to create linked list at run time?
Your linked list would be created at runtime. But why do you want to name it at runtime?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 15th, 2006, 08:52 AM
#9
Re: List Class
Because, I am reading a file that has names and I need to make a linked list for each name.
-
May 15th, 2006, 09:23 AM
#10
Re: List Class
Have a map with the following specifications:
Code:
std::map<std::string, std::list<whatever> > myMapOfLists;
This will help you achieve what you want. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 15th, 2006, 09:23 AM
#11
Re: List Class
 Originally Posted by HAOBBOY
Because, I am reading a file that has names and I need to make a linked list for each name.
In that case, you can use a std::map<std::string, std::list<std::string> >
The key of the map, would be the "name", and the value, would be the list.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
May 15th, 2006, 09:32 AM
#12
Re: List Class
 Originally Posted by Paul McKenzie
Then you need to keep track of the iterator to an element in the list, not pointer to an element in the list.
Basically when dealing with std::list, you cannot use pointers to address elements, only iterators. Yes, a list::iterator may be implemented as a pointer, but iterators and pointers are not the same thing.
Even when adding or removing elements in a list, references (and pointers) to other objects are not at all invalidated.
For example, lib.list.modifiers says that
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
void pop_front();
void pop_back();
void clear();.
Effects:
Invalidates only the iterators and references to the erased ele-
ments.
Throws:
Nothing.
And, having pointers to items of any container (except std::vector<bool> which does not met container requirements), it is perfectly valid to have references to items inside the container, as long as no modifying functions are called.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
May 15th, 2006, 09:41 AM
#13
Re: List Class
Can I do nested maps
Code:
std::map<std::string, std::map<std::string,std::string> > myMapOfLists;
the above gives me error
-
May 15th, 2006, 09:47 AM
#14
Re: List Class
Guys/Gals,
I managed to to have nested map, but how can I add data to the 2nd map then add this to the mother map?
Also, how can I reference the elements inside these maps?
-
May 15th, 2006, 10:07 AM
#15
Re: List Class
How can I declare an iterator for the nested map?
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
|