title: C++ using auto vs a defined iterator
body: <p>Could anyone explain the differences in the for loops in my code? I understand that an iterator is a pointer to some data, and when I type "*iter" I'm accessing the value that this pointer points to (an int). But I don't understand why auto doesn't work the same way here. Why don't I need to dereference vertex in the other for loop?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;queue&gt;
  
  class Graph {
      public:
          Graph(int num_v): num_vertices(num_v), adj_list(new std::list&lt;int&gt;[num_v]) {}
  
          void add_edge(int u, int v) { adj_list[u].push_back(v); }
  
          void bfs_traversal(int start) {
              bool* visited = new bool[num_vertices];
              for (int i = 0; i &lt; num_vertices; i++)
                  visited[i] = false;
              std::queue&lt;int&gt; q;
              visited[start] = true;
              q.push(start);
              //std::list&lt;int&gt;::iterator iter;
              while(! q.empty()) {
                  int node = q.front();
                  std::cout &lt;&lt; node &lt;&lt; " ";
                  q.pop();
  
                  /*
                  for (iter = adj_list[node].begin(); iter != adj_list[node].end(); ++iter) {
                      if (! visited[*iter]) {
                          visited[*iter] = true;
                          q.push(*iter);
                      }
                  */
  
                  for (auto vertex : adj_list[node]) {
                      if (! visited[vertex]) {
                          visited[vertex] = true;
                          q.push(vertex);
                      }
                  }
  
              }
          }
  
      private:
          int num_vertices;
          std::list&lt;int&gt;* adj_list;
  };
  
  int main() 
  {
      Graph graph(4);
      graph.add_edge(0,1);
      graph.add_edge(0,2);
      graph.add_edge(1,2);
      graph.add_edge(2,0);
      graph.add_edge(2,3);
      graph.add_edge(3,3);
  
      std::cout &lt;&lt; "breadth first traversal starting from vertex 2\n";
  
      graph.bfs_traversal(2);
  
      return 0;
  }
  </code></pre>
  
  <p>compile with: g++ -std=c++11 
  source: <a href="https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/</a></p>