0

I am working on some graph problem. I have:

vector<vector<int>> e 

that I populate as:

for(vector<int> edge: edges) { e[edge[0]].push_back(edge[1]); e[edge[1]].push_back(edge[0]); } 

Now when I try to access e using a range based for loop like:

for(vector<int> v: e[node]) 

I get an error:

no viable conversion from int to vector<int>

Which I guess means I should use:

for(int i: e[node]) 

How - isn't each element of e a vector?

6
  • Open ended - sometimes I find it difficult to visualize complex data-structures like vector of unordered_maps, etc. Any resources that would teach me to visualize them? Commented May 13, 2020 at 4:41
  • 2
    You're not iterating over e, you're iterating over one element of e (a vector), and each element of that element of e is an int. Commented May 13, 2020 at 4:43
  • Yes, so each element of e is a vector, so shouldn't I have to use vector<int> instead of just int? Commented May 13, 2020 at 4:44
  • Teachyou to visualize, I dunno. I just use a pencil (a 0.5mm Papermate G-Force) and paper (no special requirements). Commented May 13, 2020 at 4:46
  • 1
    for (const std::vector<int>& edge : edges)or for (const auto& edge : edges) would avoid unnecessary copies over your for(vector<int> edge : edges). Commented May 13, 2020 at 8:14

1 Answer 1

1

each element of e is a vector, each element of e[node] is int.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.