I'm simulating the optimal page replacement algorithm in C++. A huge number of page requests formatted as (hex, 'w' or 'r') is to be preprocessed to set up a chain of requests for each page at different time steps.
Currently I'm doing:
ifstream in(argv[1]);
unordered_map<unsigned int, queue<unsigned int>> distance_map;
unsigned int addr, distance = 0;
char mode;
while(in >> hex >> addr >> mode)
{
if(distance_map.find(addr) == distance_map.end())
distance_map.emplace(addr, queue<unsigned int>());
distance_map.at(addr).push(distance);
distance++;
}
It is now fine with a small input file, but the grader will test my program with "a lot larger" file. Hence, I want to allocate memory in heap. After searching for quite a while, I found even more questions:
C++ programmers suggest that do not use a pointer to an object. That is do not use
unordered_map<K, T> *map_ptr
. But I don't understand why this is a generally bad thing. What's the difference betweenmap.insert(...)
andmap_ptr->insert(...)
?Can I use
unique_ptr
for everyqueue<unsigned int>
instance that will be held? The queue is dynamically growing, so do theunsigned int
s queue contains still reside in stack?
I hope some experienced C++ programmer can tell me what to do in this scenario and answer my above two questions.