title: Create a foreach function while using a lambda function
body: <p>I have created a simulation of a LRU cache which is defined as described bellow:</p>
  
  <p><code>unordered_map&lt;string, std::pair&lt;T, std::list&lt;string&gt;::iterator&gt;&gt; mycache;</code></p>
  
  <p>Meaning, I also have a data member in the cache class called <code>list&lt;string&gt; mylist;</code></p>
  
  <p>The priorities of the cache to use LRU algorithm is on the <code>list&lt;string&gt;</code>.</p>
  
  <p>I need to create a <code>foreach</code> function using a lambda function to print out the items (<code>T objects</code>) in the cache according to their priorities (<strong>their order in <code>mylist</code></strong>).</p>
  
  <p>I did manage to print-out the items of the map:</p>
  
  <pre><code>template class &lt;p&gt;
    void foreach(p func) {
      for (typename std::unordered_map&lt;string, pair&lt;T, list&lt;string&gt;::iterator&gt;&gt;::iterator it = mycache.begin();
           it != mycache.end(); ++it) {
        func(it-&gt;second.first);
      }
    }
  </code></pre>
  
  <p>When I tried to do it on the list:</p>
  
  <pre><code> void foreach(const function&lt;void(T&amp;)&gt; func) {
      for (auto key:priorityQueue) {
        pair&lt;T, list&lt;string&gt;::iterator&gt; mypair = mycache.find(key);
        T obj = mypair.first;
        func(obj);
      }
    }
  </code></pre>
  
  <p>This fails to <strong>compile</strong> and says there's an issue with the <code>mypair</code>.
  error is the following:</p>
  
  <pre><code>error: conversion from ‘std::unordered_map&lt;std::__cxx11::basic_string&lt;char&gt;, std::pair&lt;Student, std::_List_iterator&lt;std::__cxx11::basic_string&lt;char&gt; &gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char&gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char&gt; &gt;, std::allocator&lt;std::pair&lt;const std::__cxx11::basic_string&lt;char&gt;, std::pair&lt;Student, std::_List_iterator&lt;std::__cxx11::basic_string&lt;char&gt; &gt; &gt; &gt; &gt; &gt;::iterator {aka std::__detail::_Node_iterator&lt;std::pair&lt;const std::__cxx11::basic_string&lt;char&gt;, std::pair&lt;Student, std::_List_iterator&lt;std::__cxx11::basic_string&lt;char&gt; &gt; &gt; &gt;, false, true&gt;}’ to non-scalar type ‘std::pair&lt;Student, std::_List_iterator&lt;std::__cxx11::basic_string&lt;char&gt; &gt; &gt;’ requested
         pair&lt;T, list&lt;string&gt;::iterator&gt; mypair = mycache.find(key);
  </code></pre>
  
  <p><em>Where is my bug?</em></p>