Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I preload an STL map without the map performing any rotations?

Tags:

c++

map

stl

I have sorted data coming from a database to initialize an STL map. Only 5% of data will be changed later on inside the map.

As I understand, there will be an overhead of rotations incurred for each insertions. Is it possible to bypass the overhead for sorted data? e.g. is there an option to skip rotation, and another STL algoritm to do create a balanced tree with sorted data?

PS : I am aware there will be only 2 max rotations, but was wondering if I can improve performance further.

like image 647
paseena Avatar asked May 20 '11 21:05

paseena


People also ask

Is map automatically sorted?

By default, a Map in C++ is sorted in increasing order based on its key.

How are maps implemented in STL?

STL Map Internal Implementation: It's implemented as a self-balancing red-black tree. Probably the two most common self balancing trees are red-black tree and AVL trees.

What is the use of map in STL?

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.


1 Answers

I assume you're interesting only in efficient loading of the initial sorted data?

The standard map :: map (InputIterator first, InputIterator last) constructor seems to do the right thing.

"For the iterator constructor, linear in the distance between the iterators (copy constructions) if the elements are already sorted according to comp. For unsorted sequences, linearithmic (N*logN) in that distance (sorting,copy constructions)."

like image 190
Aaron McDaid Avatar answered Nov 15 '22 13:11

Aaron McDaid