CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    How to convert c++ std::list element to multimap iterator

    Hello all
    i have
    std::list<multimap<std::string,std::string>::iterator> >

    now i have new element :
    multimap<std::string,std::string>::value_type aNewMmapValue("foo1","test")
    i want to avoid the need to set temp multimap and do insert to the new element just to get its iterator back
    so i could to push it back to the:
    std::list<multimap<std::string,std::string>::iterator> >
    can i somehow avoid this creation of the temp multimap.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How to convert c++ std::list element to multimap iterator

    If you are storing a list of multimap iterators, then the original values MUST exist in a multimap somewhere, otherwise the iterator to it will be invalid.

    Also be aware that if the multimap should re-assign it's internal storage then all of your iterators in the list will be invalidated.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: How to convert c++ std::list element to multimap iterator

    Quote Originally Posted by umen View Post
    Hello all
    i have
    std::list<multimap<std::string,std::string>::iterator> >

    now i have new element :
    multimap<std::string,std::string>::value_type aNewMmapValue("foo1","test")
    i want to avoid the need to set temp multimap and do insert to the new element just to get its iterator back
    so i could to push it back to the:
    std::list<multimap<std::string,std::string>::iterator> >
    can i somehow avoid this creation of the temp multimap.
    I'm trying to understand the reason behind you trying to convert an iterator in the first place.
    If you recall, map::insert() that takes a single argument of value_type() returns a pair,
    so you can do something like
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    #include <map>
    
    using namespace std;
    
    int main()
    {
        typedef multimap<string, string>::value_type valType;
        typedef multimap<string, string>::iterator iterType;
    
        // sample data
        multimap<string, string> mMap;
        list<iterType> List;
    
        valType viter("Key", "Value");
        List.push_back(mMap.insert(viter));
    
        return 0;
    }
    without creating a temp map, so there's no need for conversion here.
    But I could be totally wrong about what you're asking here.
    If this was way off,
    then maybe use std:air? (I haven't tried it yet) to make a bridge?
    or something like that...

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