Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list iterator + operator

Tags:

c++

stdlist

// ((++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, ...
                                               ~~~~~~~~~~~~ ^ ~
like image 532
Jookia Avatar asked Nov 03 '10 19:11

Jookia


People also ask

What is iterator operator?

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.

What is a linked list iterator C++?

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 &ltvector&gt and &ltlist&gt. A Linked list is a container class. Thus, our implementation of a linked list should also include the concept iterators.

Should I use std::list?

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.

Is std::list linked list?

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.


1 Answers

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.

like image 118
winwaed Avatar answered Nov 10 '22 16:11

winwaed