// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());
So I have the world's worst piece of code here. Is there a better way to do this?
When using '+ 1' I get this:
source/menu.cpp:146:37: error: invalid operands to binary expression
('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
'int')
menuEntries.insert(currentEntry + 1, ...
~~~~~~~~~~~~ ^ ~
One iterator object is equal to another if they address the same elements in a container. If two iterators point to different elements in a container, then they aren't equal. The first two template operators return true only if both left and right store the same iterator.
An iterator is an object that allows you to traverse a list. You can observe their usage in container classes within the C++ Standard Library such as <vector> and <list>. A Linked list is a container class. Thus, our implementation of a linked list should also include the concept iterators.
Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.
std::list is a container that supports constant time insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is usually implemented as a doubly-linked list.
Why not split into multiple lines:
iterator nextEntry = currentEntry;
menuEntries.insert( ++nextEntry, newEntries.begin(), newEntries.end());
where iterator
is iterator type for the list. Personally, I would probably pull the ++nextEntry
onto its own line for further clarity - but that is probably a subjective decision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With