CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Location
    Sydney, Australia
    Posts
    151

    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.

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    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.

    Quote 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.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    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

  5. #5
    Join Date
    Apr 2003
    Location
    Sydney, Australia
    Posts
    151

    Re: Question RE. STL's <set>

    Quote 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.

    Quote 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

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  8. #8
    Join Date
    Apr 2003
    Location
    Sydney, Australia
    Posts
    151

    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
  •  





Click Here to Expand Forum to Full Width

Featured