Get a map containing elements which are present in unordered map A but are not present in unordered map B I would like to create a function which returns a map containing differences between two unordered maps. Here is the code:

std::map&lt;std::string, int&gt; getDiff(const std::unordered_map&lt;std::string, int&gt;&amp; unordered_mapA,
                                   const std::unordered_map&lt;std::string, int&gt;&amp; unordered_mapB)
{
    std::map&lt;std::string, int&gt; mapA(unordered_mapA.begin(), unordered_mapA.end());
    std::map&lt;std::string, int&gt; mapB(unordered_mapB.begin(), unordered_mapB.end());;
    std::map&lt;std::string, int&gt; diffMap;

    std::set_difference(mapA.begin(), mapA.end(), mapB.begin(), mapB.end(), std::inserter(diffMap, diffMap.end()));

    return diffMap;
}


Edit:

A result should contain elements which are present in mapA but are not present in mapB.
At first sight, this looks good - you let a standard library algorithm and two standard library class constructors do the heavy lifting. But this approach ships with a drawback, especially the linear (given that the input is sorted) copy construction of the two local std::maps can be quite a bottleneck, e.g. when the keys are long enough to not fit into the SSO buffer. Together with std::set_difference, you have a time complexity of approximately O(N1 + N2) (neglecting constant factors) plus that of the insertion into the resulting map (which is inevitable).

You could consider a simpler, more manual approach. Recall that element lookup by a key is constant time in a hash map, so doing that inside a loop over all elements doesn't add much overhead to the function:

std::map&lt;std::string, int&gt; getDiff(const std::unordered_map&lt;std::string, int&gt;&amp; src,
        const std::unordered_map&lt;std::string, int&gt;&amp; exclude)
{
    std::map&lt;std::string, int&gt; diff;

    for (const auto&amp; node : src) {
        const auto lookup = exclude.find(node.first);

        if (lookup == src.cend() || node.second != lookup-&gt;second)
            diff.insert(node);
    }

    return diff;
}


First note that I have renamed the function arguments here. Whether src and exclude is the clearest choice might be debatable, but it should be better than unordered_mapA and unordered_mapB, which duplicates info already present in the type of these arguments. This has an improved time complexity of O(N1) (again, without constant factors or counting std::map::insert).

This can be taken one step further to make it more reusable, as there aren't any specific requirements on key and value types that should be nailed down (besides the ability to check whether objects of the value type compare equal).

template &lt;class Key, class Value&gt;
std::map&lt;Key, Value&gt; getDiff(const std::unordered_map&lt;Key, Value&gt;&amp; src,
        const std::unordered_map&lt;Key, Value&gt;&amp; exclude)
{
    // Exactly as above...
}


You can use type deduction for saving some keyboard strokes upon the invocation:

std::unordered_map&lt;std::string, int&gt; a{/* ... */};
std::unordered_map&lt;std::string, int&gt; b{/* ... */};

const auto diff = getDiff(a, b));


And one last, idiomatic level of indirection could then be to not commit to a specifiy return type, but instead pass an output iterator that accepts a std::pair&lt;Key, Value&gt;:

template &lt;class Key, class Value, class OutputIt&gt;
OutputIt getDiff(const std::unordered_map&lt;Key, Value&gt;&amp; src,
        const std::unordered_map&lt;Key, Value&gt;&amp; exclude, OutputIt dest)
{
    // As above...   

        if (...)
            dest = node; // instead of diff.insert(node);

    return dest;
}


Here, it is convention to return the output iterator at the end of the function. You can instantiate and invoke the above template as follows.

std::map&lt;std::string, int&gt; diff;
getDiff(a, b, std::inserter(diff, diff.end()));


Or, if you later decied that you need cache locality for the result of the operation:

std::vector&lt;std::pair&lt;std::string, int&gt;&gt; seqDiff;
getDiff(a, b, std::inserter(seqDiff, seqDiff.end()));


Note that you still want more abstraction, you could also hide the src argument behind input iterators, as the loop over this container isn't specific to the hash map type.