title: Implementing graph and BFS in C++ using STL
body: <p>Following is the code I have written.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;list&gt;
  #include &lt;queue&gt;
  
  using namespace std;
  
  const int V=5;
  vector&lt;list&lt;int&gt; &gt; a(V);
  
  int BFS(int s)
  {
      int visited[V]={0};
      queue&lt;int&gt; Q;
      visited[s]=1;
      Q.push(s);
      while(!Q.empty())
      {
          int x=Q.front();
          vector&lt;list&lt;int&gt; &gt;::iterator it1=a.begin()+x;
          list&lt;int&gt; it2=*it1;
          list&lt;int&gt;::iterator iter=it2.begin();
          while(iter!=it2.end())
          {
              if(visited[*iter]==0)
              {
                  visited[*iter]=1;
                  Q.push(*iter);
              }
              visited[x]=2;
              Q.pop();
              iter++;
          }
      }
      return 0;
  }
  
  void addEdge(int i, int j)
  {
      a[i].push_back(j);
      a[j].push_back(i);
  }
  
  int main() {
      vector&lt;list&lt;int&gt; &gt;::iterator it1=a.begin();
      addEdge(0,1);
      addEdge(0,2);
      addEdge(2,1);
      while(it1!=a.end())
      {
          list&lt;int&gt; it2=*it1;
          list&lt;int&gt;::iterator iter=it2.begin();
          while(iter!=it2.end())
          {
              cout&lt;&lt;*iter&lt;&lt;" ";
              iter++;
          }
          cout&lt;&lt;endl;
          it1++;
      }
      cout&lt;&lt;BFS(0);
      return 0;
  }
  </code></pre>
  
  <p>The compiler gives me a runtime error when BFS(0) is executed. Since I do not have much experience with iterators, I think the error comes from the iterator statements in the BFS function. Please help me resolve the issues in my code.</p>
  
  <p>Thank you!</p>