title: Map reprsentation of Graph
body: <pre><code>#include &lt;iostream&gt;
  #include&lt;map&gt;
  #include&lt;vector&gt;
  using namespace std;
  static int index = 0;
  template &lt;typename T&gt;
  class graph
  {
      private:
  
              typename  map&lt; T, int &gt; :: iterator iter;
              map &lt;T,int&gt; vtxMap;
              int numVertices;
              int numedges;
  
      public:
              void addEdge(graph&lt;T&gt;&amp;, const T&amp; ,const T&amp;);
              void show(graph&lt;T&gt;&amp;);
  
  
  };
  
  int main()
  {
      graph &lt;char&gt; g;
  
      g.addEdge(g,'A','B');     // add edge AB
      g.addEdge(g,'A','C');
      g.addEdge(g,'B','D');
      g.addEdge(g,'C','D');
      g.addEdge(g,'B','C');
      g.show(g);
  
      return 0;
  }
  
  template &lt;typename T&gt;
  void graph&lt;T&gt; :: addEdge ( graph&lt;T&gt;&amp; g , const T&amp; v1 , const T&amp; v2)
  {
      pair&lt;map&lt;char, int&gt;::iterator, bool&gt; ret;
        ret = g.vtxMap.insert( pair&lt;char,int&gt;('v1',index));
        if(ret.second )         // if() condition is true , means node
        {                       // gets inserted successfully. Now 
            index++;            // increase "index"to assign new unique
        }                       // value to new node.
  
        ret = g.vtxMap.insert( std::pair&lt;char,int&gt;('v2',index));
        if(ret.second)
        {
          index++;
        }
  
  }
  template&lt;typename T&gt;
   void graph&lt;T&gt; :: show( graph&lt;T&gt;&amp; g)
      {
         for( g.iter = g.vtxMap.begin(); g.iter != g.vtxMap.end(); g.iter++)
           {
               cout&lt;&lt; (*(g.iter)).first &lt;&lt;" - &gt;" &lt;&lt; (*(g.iter)).second &lt;&lt;endl;
           }
      }
  </code></pre>
  
  <p>Output :</p>
  
  <blockquote>
    <p>1-> 0<br>
    2-> 1</p>
  </blockquote>
  
  <p>Above program is not a complete representation of Graph.<br>
   I stumbled at initial stage so not posting other functions ( like removeEdge() , inDegree() , outDegree() , getNeighbor() etc... )<br>
  By seeing output, it seems that program executing only first input   </p>
  
  <pre><code>g.addEdge(g,'A','B)     
  </code></pre>
  
  <p>I am expecting output as     </p>
  
  <blockquote>
    <p>A -> 0<br>
    B -> 1<br>
    C -> 2<br>
    D -> 3         </p>
  </blockquote>
  
  <p>Means whenever there will be a new node, it should be inserted in <code>vtxMap</code> with a unique value ( i.e index ). If node is already present, it should not be inserted ( as map does not accept duplicates )<br>
  Where I am wrong ??     </p>