The compiler says the following error message for my simple depth first search algorithm written in C++:
error: no match for 'operator[]' (operand types are 'std::vector' and 'std::vectorstd::vector<int >::iterator {aka __gnu_cxx::__normal_iteratorstd::vector<int*, std::vectorstd::vector<int > >}') if (!visited[i])
Here is the code block:
#include<bits/stdc++.h>
...
vector<bool> visited;
vector<vector<int>> adj_matrix;
...
void dfs(int n) {
//Possible problem
if (!visited[n])
visited[n] = true;
for (vector<int> row : adj_matrix) {
for (vector<vector<int>>::iterator i = adj_matrix.begin();
i != adj_matrix.end(); ++i) {
if (!visited[i])
dfs(row[i]);
}
}
}
I also tried using a pointer as an array index, like so:
if (!visited[*n])
visited[*n] = true;
but it says the exact same thing.