Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

c++ - Why for loops does not work when I change it from foreach

and there were written code like this, I want to change for(int bfl : vectr[curr]) part. How I can do this. Thanks in advance

#include <bits/stdc++.h>

using namespace std;
vector < int > vectr[10001];
int visited[10001], distanced[10001];
int t, n, m, a, b;
void bf(int crt) {
    queue < int > qs;
    qs.push(crt);
    visited[crt] = 1;
    distanced[crt] = 0;
    while (!qs.empty()) {
        int curr = qs.front();
        qs.pop();

        for (int bfl: vectr[curr])
            if (visited[bfl] == 0) {
                qs.push(bfl);
                distanced[bfl] = distanced[curr] + 1;
                visited[bfl] = 1;
            }
    }
}
int main() {
    cin >> t;

    while (t--) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            vectr[i].clear(), visited[i] = 0;
        }
        while (m--) {
            cin >> a >> b;
            vectr[a].push_back(b);
            vectr[b].push_back(a);
        }
        bf(1);

        cout << distanced[n] << endl;
    }

}

I want to change it to simple for from foreach, like:

for(int i=0;i<a;i+)
question from:https://stackoverflow.com/questions/66051909/why-for-loops-does-not-work-when-i-change-it-from-foreach

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...