Hi,
I'm trying to implement a function that keeps track of recently used addresses. I've created a vector of maps to assign an incrementing value to each address key. That way, later on I can find the address in the map, and update its value, or find the oldest values. However, every time I go to test my program I get an "unhandled exception" due to "std::out_of_range" at the 'auto search' line:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int key = 0;
void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
{
auto search = table.at(index).find(address);
if(it != table.at(index).end())
{
table[index][address] = ++key;
}
else
{
table.at(index).insert(make_pair(address, ++key));
}
}
|
I modified it so it could then determine if the vector at the index location doesn't have any maps presently available to search through. But that produced the same error at the 'if(table.at(index).empty())' line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int key = 0;
void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
{
if(table.at(index).empty())
{
table.at(index).insert(make_pair(address, key));
}
else
{
auto search = table.at(index).find(address);
if(it != table.at(index).end())
{
table[index][address] = ++key;
}
else
{
table.at(index).insert(make_pair(address, ++key));
}
}
}
|
Even when I just try to insert a map at the index location I get the same exception thrown:
1 2 3 4 5 6
|
int key = 0;
void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
{
table.at(index).insert(make_pair(address, ++key));
}
|
I tried determining if the table was null, and looked into alternative means of adding maps to my vector. Unfortunately, the options I looked into (assign and push_back) are not available. I also looked into implementing a try-catch block, but I'm not too clear on how to properly program it, especially with the "throw" keyword.
I suspect my issue stems from me trying to access the table, however I don't truly know what's causing my problem or how to fix it. Does anyone have any ideas or suggestions regarding what I'm doing wrong and how to potentially resolve it?
Thank you!