gpt4 book ai didi

c++ 代码通过 Visual Studio c++ 和 Gcc 产生不同的输出

转载 作者:行者123 更新时间:2023-11-28 06:42:16 25 4
gpt4 key购买 nike

我有以下 C++ 程序:

//============================================================================
// 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()) i = (c).begin(); i != (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;

for (int i = 0; i < 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;
for (int i = 0; i < 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 does 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;
for (int i = 0; i < dist.size(); ++i)
{
if (dist[i] == INF || dist[i] == -1)
continue;

++away[dist[i]];
maxd = max(maxd, dist[i]);
}

for (int i = 0; i <= maxd; ++i)
{
cout << i << ": " << away[i] << endl;
}
}
}
else if (command == "associated")
{
string s, t;
ss >> s >> t;

if (!targs.count(s) || !targs.count(t))
{
cout << "target does 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;
}

我正在使用它作为输入:

add a b
add a c
add b d
add e b
add c f
add c g
add f h
add h i
add j k
associated a i
associated i a
associated f k
associated a h
connections a
connections i
add k g
associated a j
connections a
add h a
connections a
associated a h
add m
add n n
connections n
add a n
connections n

在 Visual C++ 中,代码生成此输出(它在调试或发布时这样做):

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
----------

在 gcc g++ 上,它产生以下输出:

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
----------

作为引用,我正在尝试解决这个问题:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=620&page=show_problem&problem=4574 .

知道为什么不同编译器的输入和输出会不同吗?我不相信我正在使用任何未定义的行为。

最佳答案

导致两个平台代码行为差异的原因是add 函数。你有:

int add(string &s)
{
if (targs.count(s))
return targs[s];

targs[s] = targs.size();
adjList.push_back(vi());
return targs.size()-1;
}

在该函数中,有问题的行是:

    targs[s] = targs.size();

这行代码很棘手,因为根据首先评估赋值运算符的哪一侧,您会得到不同的行为。请注意,targs[s] 涉及更改对象的函数调用。

您可以稍微更改该函数,使其在各个平台上保持一致和可预测。

int add(string &s)
{
if (targs.count(s))
return targs[s];

int size = targs.size();
targs[s] = size;
adjList.push_back(vi());
return size;
}

关于c++ 代码通过 Visual Studio c++ 和 Gcc 产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com