0

The outer for loop of the last loop should run umap.size() number of times but the loop is running for only one time.

It's kind of magic to me. Initially, it gives the expected result, but as I comment (q.push(b); s.insert(b);) the two lines present in the last loop and uncomment those two, after this, on compiling and running code the loop runs only one time.

Please help, I am stuck with a day!

I got stuck on leetcode, then I tried on my personal computer but the problem persists. I am running the code on g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0.

#include<bits/stdc++.h>

using namespace std;

int main() {
    vector<vector<int>> pre = {{1,0},{2,0},{1,2},{3,2},{7,2},{4,3},{5,4},{6,5},{7,6}};
    unordered_map <int, vector<int>> umap;
    for (auto a : pre) {
            umap[a[1]].push_back(a[0]);
    }

    queue<int> q;
    unordered_set <int> s;
    unordered_map <int, int> memo;

    int i;
    for (auto a : umap) {
        cout << a.first << " ";
        if (a.first >= 0) {
            for(auto b : umap[i] ) {
                if (!s.count(b)) {
                    q.push(b);
                    s.insert(b); 
                }
            }
        }
    }

    return 1;
}

Excepted output 6 5 4 3 0 2 actual result 6

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • 2
    Please try to use a debugger or printf-style debugging. We can help you catch this fish but being able to debug your fish... wait what? – One Man Monkey Squad Oct 08 '19 at 14:31
  • 3
    You have a very suspicious looking variable `i` there. – pstrjds Oct 08 '19 at 14:32
  • I wonder if a.second would be useful. – Kenny Ostrom Oct 08 '19 at 14:33
  • 1
    It is running for me: http://coliru.stacked-crooked.com/a/1e32021a84ae9c18 – Amadeus Oct 08 '19 at 14:35
  • Keep in mind that is dependent on an uninitialized variable Amadeus (as as already been hinted). UB does sometimes (cough cough) work. – Kenny Ostrom Oct 08 '19 at 14:40
  • 1
    `for(auto b : umap[i] )`: I cannot see that `i` is assigned nor inited anywhere. That's [Undefined Behavior](https://stackoverflow.com/a/4105123/1505939). – Scheff's Cat Oct 08 '19 at 14:51
  • @Amadeus Bad luck. U.B. is U.B. and "seems to run fine" is only an especially underhanded kind of U.B. ;-) – Scheff's Cat Oct 08 '19 at 14:53
  • @Scheff oh, an `i` not explicitly initialized. I did not see it on first time. but that seems to be a cloud area. See comments on here https://stackoverflow.com/questions/58275885/why-is-my-code-infinitely-looping-in-c-my-code-needs-to-repeatedly-prompt-the?noredirect=1#comment102919575_58275885 I, particularly, prefer to always initialize them, and I believe it is an UB too – Amadeus Oct 08 '19 at 23:49
  • @Scheff I know that and, that wasn't what I have pointed on my last comment. What I have pointed was that user with high pontuaction use to misunderstand that. Just look at the comments history on that question – Amadeus Oct 09 '19 at 09:47
  • @Amadeus OK, I see. ;-) – Scheff's Cat Oct 09 '19 at 09:55

0 Answers0