Results 1 to 5 of 5

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

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

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

    I have a vector<my_structure>. my_structure has an unique id int identifier.
    I have :
    vector.at(0).id=1;
    vector.at(1).id=134;
    ....
    vector.at(200).id=25478;

    I use this vector to add, insert, etc...

    Now, I need the fastest method to access the data by its id , so I need a method to know that is 25478 is stored at pos 200 .
    I could to use a find iterator or maybe to construct a previous 'map' .
    What can be the ideal-faster container or method, knowing that I only need to use it as a 'id' finder, not for add or insert or delete data.????
    This vector store pen-styles for drawing. For every line I'm going to draw I have to know its style ( by the id ). I'm going to have 'worlds' with more than 100.000 lines.....

    Any idea ? Thanks.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    How about replacing the vector with std::map with the int (id) as key and my_structure value.

  3. #3
    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.

  4. #4
    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?

  5. #5
    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.