Results 1 to 16 of 16

Thread: map

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default map

    hi, I need to do this but don't compile...
    Qt Code:
    1. std::map<int,Light> lightmap;
    2. lightmap.insert(1,Light());
    To copy to clipboard, switch view to plain text mode 
    I'd like every element has an index, and insert and remove they; furthermore I need take it ordered for index (int): how this?
    thanks.
    Regards

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: map

    If it's a sequence of indices from 0 (or 1, I suppose), you might want to use a vector instead. It'll have faster inserts and lookups.

    However, if you want to keep using the map, please post the entire code and the compiler error. I can't see anything wrong with the code you posted.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    Yes I need a map because I need erase eg element 4 and 6 and compact map (is this automatically?). Then I need re-insert elemt 4 and 6 in their position...
    (29): error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::i terator,const std::_Tree<_Traits>::value_type &)' : cannot convert parameter 1 from 'int' to 'std::_Tree<_Traits>::iterator'
    with
    [
    _Traits=std::_Tmap_traits<int,Light,std::less<int> ,std::allocator<std::pair<const int,Light>>,false>
    ]
    and
    [
    _Traits=std::_Tmap_traits<int,Light,std::less<int> ,std::allocator<std::pair<const int,Light>>,false>
    ]
    Last edited by jacek; 28th May 2006 at 18:43. Reason: changed [ code ] to [ quote ] to allow wrapping of long lines
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map

    std::map::insert() wants an iterator as the first parameter.

    Try this instead:
    Qt Code:
    1. std::map<int,Light> lightmap;
    2. lightmap[1] = Light();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: map

    Of course. I always forget about that. (To be honest, I think it's a bit illogical.)

    Here's a link that might come in handy: Click
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map

    Quote Originally Posted by Michiel
    To be honest, I think it's a bit illogical.
    I agree, luckily QMap::insert() requires a key instead of iterator.

  7. #7
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: map

    Of course, if you already happen to have an iterator, the insert will be faster. But they could have overloaded the function to accept a key as well.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    is it better use container Pair? thanks
    Regards

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    Hi, I'm trying to use map, but I've got some problems; I'd like to insert in _map one element at begin. Is there a way more simple to do this below?
    Qt Code:
    1. typedef map<int,Light> MAP;
    2. map<int,Light>::iterator ll;
    3. MAP _map;
    4. MAP::iterator _it;
    5. _map.insert(MAP::value_type(1,Light()));
    To copy to clipboard, switch view to plain text mode 
    Furhermore, After i inerted some elements in map I need add one other at the end and one at position eg 4; how to use insert to do this? thanks
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map

    Quote Originally Posted by mickey
    Is there a way more simple to do this below?
    Yes:
    Qt Code:
    1. _map[1] = Light();
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    Quote Originally Posted by jacek
    Yes:
    Qt Code:
    1. _map[1] = Light();
    To copy to clipboard, switch view to plain text mode 
    sorry bu i don't understand: am I inserting an object Light at position 1 or an obj Light with key 1? I need insert element at the end;
    With this:
    Qt Code:
    1. _map[1] = Light()
    2. _map[1] = Light()
    3. _map[1] = Light()
    To copy to clipboard, switch view to plain text mode 
    what am I doing? Have I inserted 3 element in map with key 1? thanks
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map

    Quote Originally Posted by mickey
    am I inserting an object Light at position 1 or an obj Light with key 1?
    The latter. There are no positions in a map.

    Quote Originally Posted by mickey
    I need insert element at the end;
    There is no "end" in a map.

    Quote Originally Posted by mickey
    Have I inserted 3 element in map with key 1? thanks
    No, you have inserted element with key 1 and then changed it twice.

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    sorry,
    Qt Code:
    1. _map[0] = Light()
    2. _map[1] = Light() // == _map.insert(make_pair(1,Light())); //is it th same??
    3. _map[2] = Light()
    To copy to clipboard, switch view to plain text mode 
    I've 3 Light; now I need to delete the element with key '1';
    Qt Code:
    1. _map.erase(_map.find(1));
    To copy to clipboard, switch view to plain text mode 
    This??
    thanks
    Regards

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

  15. #15
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map

    Quote Originally Posted by jacek
    There are no positions in a map.
    There is no "end" in a map.
    A question: here I tried the use of end for a map? is this correct? isn't there 'end'? I don't understand
    http://www.cppreference.com/cppmap/end.html
    Regards

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map

    Quote Originally Posted by mickey
    here I tried the use of end for a map? is this correct? isn't there 'end'? I don't understand
    You can treat std::map as both a map (i.e. set of pairs) and a sorted list of pairs .

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.