c++ code producing different outputs through Visual Studio c++ and Gcc -
i have following c++ program:
//============================================================================ // name : // author : bryce sandlund // version : // copyright : // description : code skeleton //============================================================================ #include <iostream> #include <iomanip> #include <set> #include <vector> #include <algorithm> #include <cmath> #include <complex> #include <cstdlib> #include <sstream> #include <list> #include <map> #include <fstream> #include <string> #include <time.h> #include <queue> #include <tuple> #include <functional> #include <unordered_set> #include <unordered_map> #define inf 1000000000 #define all(c) (c).begin(),(c).end() #define tr(c,i) for(typeof((c).begin()) = (c).begin(); != (c).end(); ++i) #define ep .00001 using namespace std; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<vi> vvi; typedef vector<vb> vvb; typedef vector<ii> vii; typedef vector<double> vd; typedef vector<vd> vvd; typedef long long ll; vvi adjlist; unordered_map<string, int> targs; int add(string &s) { if (targs.count(s)) return targs[s]; targs[s] = targs.size(); adjlist.push_back(vi()); return targs.size()-1; } void connect(int si, int ti) { if (si == ti) return; (int = 0; < adjlist[si].size(); ++i) { if (adjlist[si][i] == ti) return; } adjlist[si].push_back(ti); } vi bfs(int s) { queue<ii> q; q.push(ii(s, -1)); vi dist(adjlist.size(), inf); while (!q.empty()) { int top = q.front().first; int hops = q.front().second; q.pop(); if (dist[top] != inf) continue; dist[top] = hops; (int = 0; < adjlist[top].size(); ++i) { q.push(ii(adjlist[top][i], hops+1)); } } return dist; } int main() { int casenum = 1; cout << "case " << casenum << ":" << endl; string line; while (getline(cin, line)) { stringstream ss(line); string command; ss >> command; if (command == "add") { string s, t; ss >> s; int si = add(s); if (ss >> t) { int ti = add(t); connect(si, ti); connect(ti, si); } } else if (command == "connections") { string s; ss >> s; if (!targs.count(s)) { cout << "target not exist" << endl; continue; } int st = targs[s]; if (adjlist[st].empty()) { cout << "no connections" << endl; } else { vi dist = bfs(st); vi away(adjlist.size(), 0); int maxd = -1; (int = 0; < dist.size(); ++i) { if (dist[i] == inf || dist[i] == -1) continue; ++away[dist[i]]; maxd = max(maxd, dist[i]); } (int = 0; <= maxd; ++i) { cout << << ": " << away[i] << endl; } } } else if (command == "associated") { string s, t; ss >> s >> t; if (!targs.count(s) || !targs.count(t)) { cout << "target not exist" << endl; continue; } int si = targs[s], ti = targs[t]; vi dist = bfs(si); if (dist[ti] == inf) { cout << "no" << endl; } else { cout << "yes " << dist[ti] << endl; } } else { adjlist.clear(); targs.clear(); cout << "----------" << endl; cout << "case " << ++casenum << ":" << endl; } } cout << "----------" << endl; return 0; }
i using input:
add b add c add b d add e b add c f add c g add f h add h add j k associated associated associated f k associated h connections connections add k g associated j connections add h connections associated h add m add n n connections n add n connections n
in visual c++, code produces output (it on debug or release):
case 1: yes: 3 yes: 3 no yes: 2 0: 2 1: 4 2: 1 3: 1 0: 1 1: 1 2: 1 3: 2 4: 1 5: 2 yes: 3 0: 2 1: 4 2: 2 3: 2 0: 3 1: 5 2: 1 3: 1 yes: 0 no connections 0: 1 1: 3 2: 5 3: 1 4: 1 ----------
on gcc g++, produces output:
case 1: no no yes 0 no 0: 2 1: 2 2: 2 3: 1 0: 1 no 0: 2 1: 2 2: 2 3: 1 0: 3 1: 2 2: 2 3: 1 yes 0 ----------
for reference, trying solve problem: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&itemid=8&category=620&page=show_problem&problem=4574.
any ideas why input , output different on different compilers? don't believe using undefined behavior.
the cause of differences in behavior of code in 2 platforms add
function. have:
int add(string &s) { if (targs.count(s)) return targs[s]; targs[s] = targs.size(); adjlist.push_back(vi()); return targs.size()-1; }
in function, problematic line is:
targs[s] = targs.size();
the line tricky because depending on side of assignment operator gets evaluated first, different behavior. please note targs[s]
involves function call changes object.
you can change function little bit make consistent , predictable across platforms.
int add(string &s) { if (targs.count(s)) return targs[s]; int size = targs.size(); targs[s] = size; adjlist.push_back(vi()); return size; }
Comments
Post a Comment