vector - C++ index-to-index map -


i have list of ids (integers). sorted in efficient way application can handle them, example

9382 297832 92 83723 173934 

(this sort important in application).

now facing problem of having access values of id in vector.

for example values id 9382 located on somevectorb[30].

i have been using

const int units_max_size = 400000;  class clsunitsunitidtoarrayindex : public cbasestructure { private:     int m_content[units_max_size];     long m_size; protected:     void processtxtline(string line); public:     clsunitsunitidtoarrayindex();     int *content();     long size(); }; 

but raised units_max_size 400.000, page stack errors, , tells me doing wrong. think entire approach not good.

what should use if want locate id in different vector if "position" different?

ps: looking simple can read-in file , can serialized file. why have been using brute-force approach before.

if want mapping int's int's , index numbers non-consecutive should consider std::map. in case define such:

std::map<int, int> m_idlocations; 

a map represents mapping between 2 types. first type "key" , used lookup second type known "value". each id lookup can insert with:

m_idlocations[id] = position; // or m_idlocations.insert(std::pair<int,int>(id, position)); 

and can them using following syntax:

m_idlocations[id]; 

typically std::map in stl implemented using red-black trees have worse-cast lookup speed of o(log n). slower o(1) you'll getting huge array it's substantially better use of space , you're unlikely notice difference in practise unless you're storing gigantic amounts of numbers or doing enourmous amount of lookups.

edit:

in response of comments think it's important point out moving o(1) o(log n) can make significant difference in speed of application not mention practical speed concerns moving fixed blocks of memory tree based structure. think it's important represent you're trying (an int-to-int) mapping , avoid pitfall of premature optimization.

after you've represented concept should use profiler determine if , speed issues are. if find map causing issues should @ replacing mapping think quicker. make sure test optimization helped , don't forget include big comment representing , why needed changed.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -