c ++代码通过Visual Studio c + +和Gcc生成不同的输出 [英] c++ code producing different outputs through Visual Studio c++ and Gcc

查看:202
本文介绍了c ++代码通过Visual Studio c + +和Gcc生成不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C ++程序:

I have the 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()) 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 ++中,代码生成此输出(它在Debug或Release上是这样):

In Visual C++, the code produces this output (it does so 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
----------

在gcc g ++上, :

On gcc g++, it produces this 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
----------

解决此问题: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=620&page=show_problem&problem=4574

任何想法为什么输入和输出将不同的不同的编译器?我不相信我使用任何未定义的行为。

Any ideas why the input and output would be different on the different compilers? I don't believe I am using any undefined behavior.

推荐答案

两个平台是 add 函数。您有:

The cause of the differences in the behavior of the code in the two platforms is the add function. You 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 that function, the problematic line is:

    targs[s] = targs.size();

这行很棘手,因为根据赋值操作符的哪一边先求值, 。请注意, targs [s] 涉及更改对象的函数调用。

The line is tricky because depending on which side of the assignment operator gets evaluated first, you get different behavior. Please note that targs[s] involves a function call that changes the object.

您可以更改函数a

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生成不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆