Topological sort of a graph Please refer following graph. 



Please review following code for topological sort. 

I can sense one odd thing in my code, TS() is calling TSUtil(), still I had to write  Visited.emplace(n.first); and Sorted.emplace(n.first); in TS().

Please suggest if I could handle it better.

test() method is called from main to test the code.

I tried to follow C++ 11 standard. 

.h file

#pragma once
#include &lt;set&gt;
#include &lt;stack&gt;
#include &lt;unordered_map&gt;
#include &lt;vector&gt;

using std::set;
using std::stack;
using std::unordered_map;
using std::vector;

class TopolocialSort
{
private:
    set&lt;char&gt; Visited;
    stack&lt;char&gt; Sorted;
    unordered_map&lt;char, vector&lt;char&gt;&gt; G;
    void createGraph();
    void TS();
    void TSUtil(vector&lt;char&gt; childNode);
    void PrintTS();

public: 
    void test();
};


.cpp

#include "TopolocialSort.h"
#include &lt;iostream&gt;

using std::cout;
using std::endl;

void TopolocialSort::createGraph()
{
    G['A'].push_back('E');
    G['A'].push_back('C');

    G['B'].push_back('D');
    G['B'].push_back('C');

    G['C'].push_back('\0');

    G['D'].push_back('F');

    G['E'].push_back('F');
    G['E'].push_back('H');

    G['F'].push_back('G');

    G['G'].push_back('\0');

    G['H'].push_back('\0');


}

void TopolocialSort::test()
{
    createGraph();
    TS();
    PrintTS();
}

void TopolocialSort::PrintTS()
{
    while (!Sorted.empty())
    {

        cout &lt;&lt; Sorted.top() &lt;&lt;" ";
        Sorted.pop();
    }
}

void TopolocialSort::TSUtil(vector&lt;char&gt; childNode)
{
    for (auto n : childNode)
    {
        //will visit node only once
        if (Visited.find(n) != Visited.end())
        {
            continue;
        }

        Visited.emplace(n);
        TSUtil(G[n]);

        Sorted.emplace(n);
    }

}

void TopolocialSort::TS()
{
    for (auto n : G)
    {
        //will visit node only once
        if (Visited.find(n.first) != Visited.end())
        {
            continue;
        }

        Visited.emplace(n.first);
        TSUtil(n.second);

        Sorted.emplace(n.first);
    }
}
Small nits from my side.


You are always using for (auto foo : bar). This involves a copy of foo. If you do not alter foo then you should use for (const auto&amp; foo : bar)
As far as i know it is more idiomatic to check for existense of an element in a set via count. So rather than Visited.find(n) != Visited.end() you can check Visited.count(n) or the in my opinion way better Visited.count(n) != 1
Your code assumes every node is unique. You might want to adopt to a more robust approach utilizing a struct node that holds the data and then wr on pointers to that node