|
-
February 16th, 2007, 10:28 AM
#1
Question RE. STL's <set>
Code:
set<DWORD> someSet;
pair< set<DWORD>::Iterator, bool > prInsertNumN;
someSet.Insert(SomeValue1);
someSet.Insert(SomeValue2);
...
prNInsert = someSet.Insert(SomeValueN);
someSet.Insert(SomeValueN+1);
someSet.Insert(SomeValueN+2);
...
//prNInsert.first has become invalid...
Basically- I'd like the someSet iterator prNInsert to stay pointing to the SomeValueN element, even though things are being inserted/removed in the set to which it belongs before and after it.
Is this even possible- and ny ideas how I may do this?
Thanks heaps
Last edited by Metro_Mystery; February 16th, 2007 at 10:35 AM.
-
February 16th, 2007, 10:49 AM
#2
Re: Question RE. STL's <set>
Well set is used for the storage and retrieval of data from a collection in which the values of the elements contained are unique and serve as the key values according to which the data is automatically ordered. So when u insert or delete in Set the insert member function returns an iterator that points to the position where the new element was inserted into the set. On insertion and deletion the container will be reordered so ur Iterator may point to someother value on that position.
Use iterator find( const Key& _Key) const; funtion when u want the Iterator of such value.
A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.
NAUMAAN
-
February 16th, 2007, 03:18 PM
#3
Re: Question RE. STL's <set>
Actually that is wrong. The only operation which invalidates a std::set iterator is when the element you're pointing to has been removed. Adding other elements or removing other elements doesn't matter. So unless you are removing the element that you added beforehand, you will be fine.
 Originally Posted by SGI's STL docs
Set has the important property that inserting a new element into a set does not invalidate iterators that point to existing elements. Erasing an element from a set also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 16th, 2007, 05:07 PM
#4
Re: Question RE. STL's <set>
That's a new one on me. I was under the same impression as Abdul, that sets were typically implemented as balanced binary trees. Which is what Josuttis says in his book "The C++ Standard Library". Iterators will be invalidated if you insert/delete from a balanced binary tree. Well, let me re-phrase that, iterators *could* be invalidated (depending on the implementation of the iterator).
Viggy
-
February 16th, 2007, 06:34 PM
#5
Re: Question RE. STL's <set>
 Originally Posted by MrViggy
Which is what Josuttis says in his book "The C++ Standard Library". Iterators will be invalidated if you insert/delete from a balanced binary tree. Well, let me re-phrase that, iterators *could* be invalidated (depending on the implementation of the iterator).
Great book, the same one I'm learning from... I'm sure I too remember reading something like that.
 Originally Posted by Yves M
Actually that is wrong. The only operation which invalidates a std::set iterator is when the element you're pointing to has been removed. Adding other elements or removing other elements doesn't matter. So unless you are removing the element that you added beforehand, you will be fine.
I'll check it out now and see if this works in Visual Studio 2003. I'll let you all know...
Thanks for the great replies everyone
-
February 18th, 2007, 01:05 AM
#6
Re: Question RE. STL's <set>
Moving of the set elements would only alter the pointers to other nodes. set is a node based container. The object in specific memory location would remain intact because there is no reallocation.
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++?
-
February 18th, 2007, 01:12 AM
#7
Re: Question RE. STL's <set>
and see if this works in Visual Studio 2003.
And this issue has NO BEARING ON VISUAL STUDIO OF ANY VERSION
It has to do with the implementation of STL in the support libraries which is completely independant. Even if you use notepad to edit your files and compile with command line compilers [and I am talking about the same compilers that Visual Studio happens to use], the implementation of std::set will either follow the standard or not. The IDE has nothing to do with it.
Which ius why this question is properly posted in this forum.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 18th, 2007, 06:40 AM
#8
Re: Question RE. STL's <set>
Heh, yeah didn't mean it that way...
Thanks everyone- it works fine and solves the problem beautifully
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
|