Im working on a project in c++ and in this segment of code: // Puts key-value pa
ID: 3798595 • Letter: I
Question
Im working on a project in c++ and in this segment of code:
// Puts key-value pair into the table
virtual void put(const Key& key, const Value& val = Value{})
{
// create key-value pair
NodePair newNode = NodePair(key, val);
if (_pairs.empty())
{
_pairs.push_front(newNode);
return;
}
// insert pair into proper (sorted) position
std::deque<NodePair>::const_iterator it = _pairs.begin();
while (it != _pairs.end())
{
if (keyLessThan(*it, newNode))
{
it++;
}
else
{
_pairs.insert(it, newNode);
}
}
_pairs.push_back(newNode);
}
I am getting a "iterator + offset out of range" error. What do I need to do to fix this?
Explanation / Answer
I read the code segment you have uploaded. You are using a deque from the standard template library and performing operations over it. The error you mentioned iterator + offset out of range comes when you are trying to access element with an index and which is not possible index.
For example, for array arr[5], if you access arr[5], it will give an error as array index is out of the bound. Because array index starts from 0 and goes till size -1.
Same is the case with deque.
Here, you are inserting the key-value pair at appropriate position and sorting them along. At first, you are checking if _pairs is empty. If it is, then you need to insert it in the first position or the else part. Till this, it is ok.
The error I think comes because of the while loop condition. You have started from begin() and until you reach the end of _pairs, you are iterating and if the present key is less than newNode, you are simply incrementing variable it. Otherwise it is inserted in _pairs if newNode having key is less than the key.
So, this loop continues till you get last element of entries, and after that if every time if condition is satisfied, you will have it++ for every time, so it will contain a location index which is same as length of deque, and after that you are pushing it back. So, it tries to save the value at last + 1 location which may cause error.
So, I would suggest you that you don't increment the it when it reaches for the last time.
So, to remove this error, iterate the loop till previous of _pair.end() in the while loop. In short, while pushing back the value, first check if it points to end() or not. If it points, decrement it by 1 and then perform insertion. In short, write a checking condition before performing _pairs.push_back(newNode);
The modified code as per me is:
// Puts key-value pair into the table
virtual void put(const Key& key, const Value& val = Value{})
{
// create key-value pair
NodePair newNode = NodePair(key, val);
if (_pairs.empty())
{
_pairs.push_front(newNode);
return;
}
// insert pair into proper (sorted) position
std::deque<NodePair>::const_iterator it = _pairs.begin();
while (it != _pairs.end())
{
if (keyLessThan(*it, newNode))
{
it++;
}
else
{
_pairs.insert(it, newNode);
}
}
if(it ==_pairs.end())
it--;
_pairs.push_back(newNode);
}
This should work. This is what I can derive from the given code. If there is a query or if you find this complex after it is running, please comment. I will solve it for sure. Thank you. :)