【问题标题】:C++ beginner declaring a function involving array of structuresC++初学者声明一个涉及结构数组的函数
【发布时间】:2017-07-01 02:39:28
【问题描述】:

有几行代码我想定义为一个函数,因为我打算多次使用它。部分代码如下:

// loop within loop used to reorder with highest price at the top
for(i=0;i<x;i++){
    for(t=i;t<x;t++){
        if(f[i].price < f[t].price) {
            temp = f[i].price;
            f[i].price = f[t].price;
            f[t].price = temp;
        }

    }
}

我希望每次调用函数时都能为 x 和 f 输入新值。我在下面包含了我的所有代码。如果我无论如何都不清楚我的目标,请随时提问。我提前为不恰当的术语道歉,我对此并不陌生。谢谢

#include <iostream>
using namespace std;

struct List
    {
        char name[10];
        int price;
        };

int main()
{
    //x represents number of structures within array!
     int x; 

    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;


    //array of structures fruit
    struct List f[x];

    int i;

    //input values into structure
    for (i = 0; i < x; i++) {
      cout << "\nEnter fruit name, and price.\n";
      cin >> f[i].name;
      cin >> f[i].price;
    };

    //variables for reordering
     int temp;
     int t;


    // loop within loop used to reorder with highest price at the top
    for(i=0;i<x;i++){
        for(t=i;t<x;t++){
            if(f[i].price < f[t].price) {
                temp = f[i].price;
                f[i].price = f[t].price;
                f[t].price = temp;
            }

        }
    }


    //Output of menus
    //fruit Menu
    cout << "\n\nFruit Menu";
     for (i = 0; i < x; i++) {
        cout << "\n" << f[i].name << " $" << f[i]. price;
        };


    return 0;
}

【问题讨论】:

  • 那么你的问题是什么?
  • 使用std::sort() 会好很多——冒泡排序几乎从来都不是一个好主意!
  • 提示:遵循一个相当古老的原则,即“将输入与输出分离”。所以编写一个收集输入的函数,编写一个重新排序的函数,然后编写一个打印结果的函数。然后以任何你喜欢的方式组合它们。
  • 使用C++标准containers

标签: c++ arrays function struct


【解决方案1】:

我想这是一个作业,上面写着“实现你自己的排序函数来对水果数组进行排序”,所以我将数据结构“水果数组”作为给定的。当然,您也可以将其更改为 vector&lt;struct Fruit&gt;,但这是另一个主题。

也许以下片段可以帮助您完成代码。它包含用于输入、排序和打印数组的函数以及一些如何处理参数和调用的示例。您必须完成代码。

玩得开心!

#include <iostream>
using namespace std;

struct Fruit
{
    char name[10];
    int price;
};

// enter up to nrOfFruis; return number of fruits actually entered
int enterFruits(struct Fruit *fruits, int maxNrOfFruits) {
    int entered = 0;
    while (entered < maxNrOfFruits) {

        cin >> fruits[entered].name;

        entered++;
    }
    return entered;
}

void sortFruits(struct Fruit* fruits, int nrOfFruits) {

    // your sort code goes here

    // example for swaping two elements:
    Fruit temp = fruits[0];
    fruits[0] = fruits[1];
    fruits[1] = temp;
}

void printFruits(struct Fruit *fruits, int nrOfFruits) {

    cout << "\n\nFruit Menu";
    for (int i = 0; i < nrOfFruits; i++) {
        cout << "\n" << fruits[i].name << " $" << fruits[i]. price;
    };
}



int main()
{
    // Your task: put a proper loop and exit condition arround the following lines...

    int x;
    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;

    struct Fruit fruits[x];
    int entered = enterFruits(fruits, x);
    sortFruits(fruits, entered);
    printFruits(fruits, entered);

    return 0;
}

【讨论】:

    【解决方案2】:

    如果在编译时不知道数组的大小,则无法在堆栈上分配数组。因此,你需要为它动态分配内存(你也需要记得删除它):

    //x represents number of structures within array!
     int x; 
    
    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;
    
    
    //array of structures fruit
    struct List * f = new List[x];
    
    //...
    
    delete [] f;
    

    或者,您可以使用 C++ 方式,使用向量,将向量元素放在堆栈上:

    int x;
    std::cin>>x;
    std::vector<A> v(x);
    
    for( size_t i = 0; i < x; i++)
    {
        std::cin >> v[i].x;
    }
    

    【讨论】:

      【解决方案3】:

      如果您只想将数组传递给函数,您可以这样做:

      void sortArray(struct List list[]);
      void sortArray(struct List* list, int n); // n = size of array
      

      最好只使用 std::vector 或其他一些列表容器。 :)

      【讨论】:

        【解决方案4】:

        听起来你想要一个接收数组 t 和索引 x 的函数,并且你想改变函数中的数组?

        C++ 是“按值传递”,因此要改变数组,您必须让函数获取对数组的引用(或指针),以便改变原始数组而不是它的副本,所以只需你的函数签名是这样的:func(T&amp; t, int x)(假设 T 是数组 t 的类型)。

        【讨论】:

        • 该函数的目的是按价格对数组中的结构进行排序,它成功了。但是,我计划重复使用这几行排序代码,因为我想按价格对其他菜单进行排序,即乳制品、饮料等。对不起,我的术语不好,但 x 是数组的结构数。 t 只是我用于排序代码的随机变量, f 指的是要排序的数组。我希望在调用函数时能够更改的值是 f 和 x,以便函数可以对具有不同数量结构的其他结构数组进行排序。
        • 不确定这是否有意义,但我感谢您的努力。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 2016-02-19
        相关资源
        最近更新 更多