What's going wrong? Please help.

vector< vector > adj; // vector of vectors adj
vector< vector >::iterator it;
for( it = adj[node].begin(); it != adj[node].end(); ++it ); // line xyz
{
   int val = adj[node][*it];                              // line pqr
}

Errors:
--------
1> line xyz: no match for ‘operator =’ in ‘it = (& adj.std::vector<_Tp, _Alloc>::operator[]<std::vector, std::allocator<std::vector > >(((std::vector<std::vector >::size_type)node)))->std::vector<_Tp, _Alloc>::begin<int, std::allocator >()’

2> line xyz: no match for ‘operator !=’ in ‘it = (& adj.std::vector<_Tp, _Alloc>::operator[]<std::vector, std::allocator<std::vector > >(((std::vector<std::vector >::size_type)node)))->std::vector<_Tp, _Alloc>::begin<int, std::allocator >()’

3> line pqr : no match for ‘operator[]’ in ‘adj.std::vector<_Tp, _Alloc>::operator[]<std::vector, std::allocator<std::vector > >(((std::vector<std::vector >::size_type)node))[it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<std::vector*, std::vector<std::vector > >()]’
4 Likes

The begin() function returns an iterator of the type vector so i should also be of the same type
and that is why there is an complilation error.
if you want to iterate through the vector the code to be used:

vector <int> v;
vector <int> :: iterator it;
for(it=v.begin();it!=v.end();it++);

In case of vectors of vectors you would need two iterators one of type vector< vector > and another of the type vector .

For eg:

vector < vector <int> > v;
vector < vector <int> > :: iterator ii;
vector < int > :: iterator ij;
for(ii=v.begin();ii!=v.end();ii++)
{
    for(ij=(*ii).begin();ij!=(*ii).end();ij++)
    cout<<*ij;
}

Edit:
If you only want to access only the elements of vector v[x] then code would be :

vector < vector <int> > v;
vector < vector <int> > :: iterator ii;
vector < int > :: iterator ij;
ii = v.begin()+x;

for(ij=(*ii).begin();ij!=(*ii).end();ij++)
cout<<*ij;

In your case you could replace :

val = adj[node][*it];//Replace it by val = *it
3 Likes

UgXpyN - Online C++ Compiler & Debugging Tool - Ideone.com → This is the code for FIRESC Problem. I am getting RTE. Above question was from this code only. Please check if you have time :slight_smile:

The link is not public.

Ooops…check now :slight_smile:

http://ideone.com/BHhpCl
This should be working code.

The things I have added first adj.resize(n+1) and in BFS count should be incremented in the if(!visited[val]) part

Have you checked my link.

http://ideone.com/BHhpCl I am telling you to see this link it has all the corrections

Sorry, all the comments were not visible. Saw just a couple of mins ago.