Results 1 to 5 of 5

Thread: What can be the fastest STL container to manage ordered and not-continuos data ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What can be the fastest STL container to manage ordered and not-continuos data ?

    Thanks Zlato. Yes, I know that possibility.
    But, is there any fast alternative to search a value by its id ?( I need only search, not insert and more functionalities)

    Thanks again.

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What can be the fastest STL container to manage ordered and not-continuos data ?

    you have a key (the id) and an associated value (the index). that's exactly whats std::map is for! why do you need an alternative?

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What can be the fastest STL container to manage ordered and not-continuos data ?

    If you chose the std::vector, then to find an element with given id, you can perform a half-interval (binary) search on your vector (I assume that elements in vector are sorted by the id, as in your example). Worst-case performance of binary search is O(log n) - same as find() on std::map.
    If the values will not be sorted, I suggest using std::map with id as key, because using std::vector, in order to find the given element, you will need to perform linear search on the container ( complexity O(n), its slower than O(log n) ).
    To summarize: if elements in vector are sorted by id, use std::vector and binary search. If not, use std::map (if you really care about the find() speed).
    ---
    probably you will get similar results using those two approaches ( you can do some performance tests ), in that case using std::map is far more convenient.

Similar Threads

  1. Replies: 5
    Last Post: 29th May 2014, 09:11
  2. Replies: 6
    Last Post: 27th March 2010, 05:42
  3. Replies: 6
    Last Post: 22nd March 2010, 08:57
  4. QHash ordered by value
    By Lele in forum Qt Programming
    Replies: 7
    Last Post: 11th February 2008, 09:30
  5. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 11:20

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.