Checking if the entries of a map exist in another map I need to check if a map is subset of another map. For this, I have written the following code:

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;map&gt;

using namespace std;

/**
 * Checks if rhs is a subset of lhs
 */
template&lt;class Map&gt;
bool map_compare(Map &amp;lhs, Map &amp;rhs)
{
    if(rhs.size() &gt; lhs.size() || rhs.empty()) return false;
    typename Map::iterator litr = lhs.begin();
    for (typename Map::iterator ritr = rhs.begin(); ritr != rhs.end(); ritr++)
    {
        if(litr == lhs.end() &amp;&amp; ritr != rhs.end()) return false;
        while (litr != lhs.end())
        {
            if (litr-&gt;first == ritr-&gt;first)
            {
                if (litr-&gt;second == ritr-&gt;second)
                {
                    litr++;
                    break;
                }
                return false;
            }
            else
            {
                litr++;
            }
        }
    }
    return true;
}

int main()
{
    map&lt;int, string&gt; a, b;

    a[0] = "0";
    a[1] = "1";

    b[0] = "0";

    cout &lt;&lt; "b ⊆ a? " &lt;&lt; map_compare(a, b) &lt;&lt; " (should be 1)\n";

    b[1] = "1";
    cout &lt;&lt; "b ⊆ a? " &lt;&lt; map_compare(a, b) &lt;&lt; " (should be 1)\n";

    b[2] = "2";
    cout &lt;&lt; "b ⊆ a? " &lt;&lt; map_compare(a, b) &lt;&lt; " (should be 0)\n";
}


I am looking for another implementation of map_compare which is either shorter or more efficient.
I believe you will find the standard library has the appropriate functions:
Includes

Includes tests whether one sorted range includes another sorted range. That is, it returns true if and only if, for every element in [first2, last2), an equivalent element [1] is also present in [first1, last1) [2]. Both [first1, last1) and [first2, last2) must be sorted in ascending order.
The two versions of includes differ in how they define whether one element is less than another. The first version compares objects using operator&lt;, and the second compares objects using the function object comp.

You can see all the algorithms here
Your code would look like this:
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;map&gt;

int main()
{
    std::map&lt;int, std::string&gt; a;
    std::map&lt;int, std::string&gt; b;

    a[0] = &quot;0&quot;;
    a[1] = &quot;1&quot;;

    b[0] = &quot;0&quot;;

    std::cout &lt;&lt; &quot;b ⊆ a? &quot; &lt;&lt; std::includes(a.begin(), a.end(), b.begin(), b.end()) &lt;&lt; &quot; (should be 1)\n&quot;;

    b[1] = &quot;1&quot;;
    std::cout &lt;&lt; &quot;b ⊆ a? &quot; &lt;&lt; std::includes(a.begin(), a.end(), b.begin(), b.end()) &lt;&lt; &quot; (should be 1)\n&quot;;

    b[2] = &quot;2&quot;;
    std::cout &lt;&lt; &quot;b ⊆ a? &quot; &lt;&lt; std::includes(a.begin(), a.end(), b.begin(), b.end()) &lt;&lt; &quot; (should be 0)\n&quot;;
}