【问题标题】:Not getting Desired Output (Struct with Array) C++ [closed]没有得到所需的输出(带数组的结构)C++ [关闭]
【发布时间】:2017-09-23 17:34:05
【问题描述】:

这是我的代码:

#include <iostream>

using namespace std;

struct galaxy
{
    int x_pixel;
    int y_pixel;
};

galaxy get_space_ship(int size)
{
    galaxy UFO[size];
    for(int i=0;i<size;i++)
    {
        UFO[i].x_pixel = 10;
        UFO[i].y_pixel = 10;
    }
    return UFO[size];
}

galaxy update(galaxy UFO[], int size)
{
    for(int i=0;i<size;i++)
    {
        UFO[i].x_pixel += 100;
        UFO[i].y_pixel += 100;
    }
    return UFO[size];
}

int main()
{
    galaxy space_ship[10];
    space_ship[10] = get_space_ship(10);
    for(int i=0;i<10;i++)
    {
        cout << space_ship[i].x_pixel << endl;
        cout << space_ship[i].y_pixel << endl;
    }
    space_ship[10] = update(space_ship,10);
    for(int i=0;i<10;i++)
    {
        cout << space_ship[i].x_pixel;
        cout << space_ship[i].x_pixel;
    }
}

在这里,我正在尝试为一个数组创建一个具有两个字段(x_pixel,y_pixel)的结构的程序,然后初始化它们并更新它们的值并打印它们但没有得到预期的输出。我的代码没有任何编译错误。请检查我的代码,让我知道我错在哪里,并包括一些提示,因为我是新手。 谢谢

【问题讨论】:

  • C 样式数组不支持您使用它们的语义。试试std::array。或者,std::vector 用于非静态尺寸。
  • galaxy space_ship[10]; space_ship[10] = ... - 此处未定义的行为。 space_ship 只有 10 个元素,因此有效索引为 0 .. 9。
  • 使用 std::vector!
  • galaxy UFO[size]; -- 这不是有效的 C++。 C++ 中的数组必须使用常量表达式来表示条目数,而不是变量。正如多个 cmets 和答案所建议的那样,使用 std::vector

标签: c++ arrays struct


【解决方案1】:

C 中的数组不像你想象的那样工作。他们不是一等公民,因此您不能按价值传递或归还他们。数组会衰减为指向其第一个元素的指针。这是一件好事,因为在您的示例中,您不想来回复制整个数组这么多次。这是一个可以满足您要求的工作示例:

struct galaxy
{
    int x_pixel;
    int y_pixel;
};

void get_space_ship(galaxy* arr, int size)
{
    for (int i = 0; i < size; i++)
    {
        arr[i].x_pixel = 10;
        arr[i].y_pixel = 10;
    }
}

void update(galaxy* arr, int size)
{
    for (int i = 0; i < size; i++)
    {
        arr[i].x_pixel += 100;
        arr[i].y_pixel += 100;
    }
}

int main()
{
    galaxy space_ship[10];
    get_space_ship(space_ship, 10);
    for (int i = 0; i < 10; i++)
    {
        cout << space_ship[i].x_pixel << endl;
        cout << space_ship[i].y_pixel << endl;
    }
    update(space_ship, 10);
    for (int i = 0; i < 10; i++)
    {
        cout << space_ship[i].x_pixel;
        cout << space_ship[i].x_pixel;
    }
}

但是,由于这些原因,C 数组被认为是一种非常低级且容易出错的构造。更好的方法是使用 std::vector 或 std::array 并通过引用传递它们以保存副本。

std::vector<galaxy> get_space_ship(int size)
{
    std::vector<galaxy> ships;

    for (int i = 0; i < size; ++i)
        ships.push_back(galaxy{ 10, 10 });

    return ships;
}

void update(std::vector<galaxy>& ships) // note reference
{
    for (auto & ship : ships)
    {
        ship.x_pixel += 100;
        ship.y_pixel += 100;
    }
}

int main()
{
    auto ships = get_space_ship(10);

    for (const auto & ship : ships)
    {
        cout << ship.x_pixel << endl;
        cout << ship.y_pixel << endl;
    }

    update(ships);

    for (const auto & ship : ships)
    {
        cout << ship.x_pixel;
        cout << ship.x_pixel;
    }
}

请注意,您不再需要多次声明大小,因为您使用的是知道其大小的更高级别的抽象。

【讨论】:

  • 绝对没有理由使用在 C++ 中衰减为指针的数组。有更安全、更清晰的替代方案。
  • 好吧,我在解释数组是如何工作的,你是说他/她不应该知道这个?
  • 也许 OP 应该知道这一点,但肯定不会使用它。您能否添加一个与数组引用一起使用的小样本(例如get_space_ship),以及返回(RVO'd)std::arraystd::vector 的更好的样本?我相信它会极大地帮助 OP 学习惯用的 C++ 方法来传递数组。
  • @patatahooligan 没有问题,完成。
  • 看起来很棒!附带说明一下,您可以像for (auto&amp; ship : ships) 这样遍历向量,然后执行ship.x += 100; 等。它是等效的,但更干净。不过,仅适用于 C++11 或更高版本,但这在大多数平台上都不是问题。
【解决方案2】:

您对以下 c++ 结构有误解:

  • space_ship[10] 并不意味着“所有十艘宇宙飞船”。它表示索引为 10 的宇宙飞船(=第十一艘宇宙飞船)。你没有那么多的宇宙飞船,你有十个。
  • 如果您不熟悉指针,请避开普通的 C++ 数组,它们会出现一些令人惊讶的行为,迫使您使用指针。特别是很难将它们作为一个整体进行处理。如果您完全不熟悉 C/C++,我建议您使用 std::vectorstd::array(对于固定数量的元素),这对您来说更容易预测。
  • 如果您想继续使用常规 [] 数组,请注意 galaxy_update 不会收到数组的副本,它会直接修改数组。不需要返回修改后的数组。

建议:一定要正确缩进你的代码。一个合适的 IDE 将帮助您。缩进可能看起来很麻烦,但它的成本/收益比很高。

【讨论】:

  • 谢谢。期待您的建议。
猜你喜欢
  • 2015-05-16
  • 1970-01-01
  • 2016-12-12
  • 2014-01-24
  • 2014-03-24
  • 1970-01-01
  • 2022-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多