Attempt at checking whether two words are permutations This is my first algorithm in C++ ever to check if two strings are permutations of each other. Please provide guidance on correctness of algorithm + ways to improve + thoughts on my use of the language itself.

The idea is two create two dictionaries which count the number of occurrences of each character using two for loops, and then we use a final for loop to check if the counts are equal.

void checkPermut(std::string &amp;sentence1, std::string &amp;sentence2)
{
    std::map&lt;char, int&gt; dict1;
    std::map&lt;char, int&gt; dict2;

    for (char &amp; c : sentence1) 
    {
        if (dict1.find(c) == dict1.end())
        {
            dict1.insert(std::pair&lt;char, int&gt;(c, 1));
        }
        else
        {
            dict1[c] == dict1[c] + 1;
        }
    }

    for (char &amp; c : sentence2)
    {
        if (dict2.find(c) == dict2.end())
        {
            dict2.insert(std::pair&lt;char, int&gt;(c, 1));
        }
        else
        {
            dict2[c] == dict2[c] + 1;
        }
    }

    bool arePermut = true;

    for (char &amp; c : sentence2)
    {
        if (dict1.find(c) == dict1.end())
        {
            arePermut = false;
            std::cout &lt;&lt; "Not permutations." &lt;&lt; std::endl;
            break;
        }
        else if (dict1[c] != dict2[c])
        {
            arePermut = false;
            std::cout &lt;&lt; "Not permutations." &lt;&lt; std::endl;
            break;
        }
    }

    if (arePermut)
    {
        std::cout &lt;&lt; "Are permutations" &lt;&lt; std::endl;
    }

}

int main()
{
    std::string somestring1;
    std::string somestring2;

    std::cin &gt;&gt; somestring1 &gt;&gt; somestring2;
    checkPermut(somestring1, somestring2);

    return 0;
}
Basic Review

Look below for algorithm ideas:

You are not modifying the input strings. So they should be passed by const reference to prevent accidents:

void checkPermut(std::string const&amp; sentence1, std::string const&amp; sentence2)
                             ^^^^^                         ^^^^^


You don't need to check if a value exists in a map like this:

        if (dict1.find(c) == dict1.end())
        {
            dict1.insert(std::pair&lt;char, int&gt;(c, 1));
        }
        else
        {
            dict1[c] == dict1[c] + 1;
        }


Use operator[] If it does not exist the item will be created (the value is "zero" initialized" and returned as a reference. You can replace the above code with the line:

        ++dict1[c];


Be careful when getting things by reference (accidental modification can happen). Unless you need to modify something, get by const reference.

    for (char const&amp; c : sentence2)
              ^^^^^


But in this case I would get by value:

    for (char c : sentence2)


There is an easy way to make pairs that does not require all the type info:

            std::pair&lt;char, int&gt;(c, 1);


Simpler to write:

            std::make_pair(c, 1); // The function gets the type from
                                  // the parameters and creates the
                                  // correct type of underlying pair.


As a side note. technically you may not want to make the assumption that std::map uses a pair internally. This is why std::map has an internal type value_type so you can use that and not make any assumptions about the type used internally.

 using Map      = std::map&lt;char, int&gt;;
 using MapValue = typename Map::value_type;
 Map  dict1;
 ...
 dict1.insert(MapValue(c, 1));


There is a subtle bug here:

    for (char &amp; c : sentence2)
    {
        if (dict1.find(c) == dict1.end())
        {
            arePermut = false;
            std::cout &lt;&lt; "Not permutations." &lt;&lt; std::endl;
            break;
        }
        else if (dict1[c] != dict2[c])
        {
            arePermut = false;
            std::cout &lt;&lt; "Not permutations." &lt;&lt; std::endl;
            break;
        }
    }


You check all the letters from sentence2 are exactly the same as in sentence1. But that may not be enough. You need to check the other way.

    // Assume: 
    Sentence1 = "BigZ"
    Sentence2 = "Big";  // Your algorithm will match.


Note most containers are comparable.

There is a subtle bug here:

        if (dict1 != dict2)
        {
            std::cout &lt;&lt; "Not permutations." &lt;&lt; "\n";
        }
        else
        {
            std::cout &lt;&lt; "Not permutations." &lt;&lt; "\n";


Note: The operator&gt;&gt; reads a single space separated word. Your code above suggests sentences. Is this what you want?

    std::cin &gt;&gt; somestring1 &gt;&gt; somestring2;


Use std::getline(std::cin, somestring1) to read a line.

This is redundant in main():

    return 0;


Algorithm

We know you can simply compare the containers. But you can optimize this. By subtracting characters from sentence two from the container. If any become negative then you can exit early.

Also do you want to compare space characters? This makes it really hard to have anagram sentences if the word count has to be the same.

bool checkPermut(std::string const&amp; sentence1, std::string const&amp; sentence2)
{
    using Map = std::map&lt;char, int&gt;;
    Map count1;
    Map zero;

    for(auto c: sentence1) {
        if (std::isspace(c) {
            continue;
        }
        ++count1[c];
        zero[c] = 0;
    }
    for(auto c: sentence2) {
        if (std::isspace(c) {
            continue;
        }
        if (--count1[c] &lt; 0) {
            return false; // exit early if we go negative.
        }
        zero[c] = 0;
    }
    return count1 == zero;  // Make sure all letters have reached zero.
}