Printing contents of list with indices from other list I have two lists: L stores some values and P holds values that are indices in the first list that should be printed. Lists are sorted in ascending order. Error-checking can be ignored, and both lists are valid and values are valid.

Do I understand correctly, that since I always continue from the place I ended at, and since the values in both lists only increase, the cost of this operation is \$O(n)\$, since I only iterate over the list once?

I'd like to clean up the code. For example, is there a way to get the current index from the iterator, that would remove two additional int variables?

#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;

using std::list;

void printLots(const list&lt;int&gt; &amp;l, const list&lt;int&gt; &amp;p);

int main(int argc, const char * argv[]) {

    list&lt;int&gt; listL;
    listL.push_back(2);
    listL.push_back(4);
    listL.push_back(6);
    listL.push_back(8);
    listL.push_back(10);
    listL.push_back(12);
    listL.push_back(14);
    listL.push_back(16);
    listL.push_back(18);
    listL.push_back(20);

    list&lt;int&gt; listP;
    listP.push_back(0);
    listP.push_back(9);

    printLots(listL, listP);
}

void printLots(const list&lt;int&gt; &amp;l, const list&lt;int&gt; &amp;p) {

    auto lIterator = l.begin();
    auto pIterator = p.begin();

    int lIndex = 0;
    int pIndex = 0;

    bool finished = false;

    while (!finished) {
        int indexToPrint = *pIterator;

        while (lIndex != indexToPrint) {
            lIndex++;
            lIterator++;
        }

        std::cout &lt;&lt; *lIterator &lt;&lt; std::endl;
        pIndex++;
        pIterator++;

        if (pIterator == p.end()) {
            finished = true;
        }
    }
}
It doesn't look like you're using &lt;stdio.h&gt;, so you can just remove it.
If you won't ever need to provide command line arguments, then you can remove main()'s parameters.
If you'll just be iterating though these lists forward, then it may be more beneficial to use an std::forward_list instead.  Or maybe you can just use an std::vector if there's no special need for a linked list.
Your list assignments is still pre-C++11.  Instead, use its initializer list:

list&lt;int&gt; listL { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

list&lt;int&gt; listP { 0, 9 };


This will also allow you to make them const right away.