【问题标题】:Sorting a data structure that points to an array c++对指向数组c ++的数据结构进行排序
【发布时间】:2016-01-08 10:54:57
【问题描述】:

好的,我现在正在处理这个项目。程序读取一个文件,该文件可以有任意数量的行,并且文件中的每个项目都是不同的类型,如下所示:

1002 Hammer       23.65  203
1024 Nails         6.95  400
1276 Screwdriver  13.95  251
1385 Elec_Drill   45.69  132
1462 Air_Filter    7.95  500

第一个数字是双精度类型的产品编号,第二个是字符串类型,然后是每个价格的浮点数,然后是销售数字的整数。程序读入这些,然后对它们进行排序并输出最高销量和最低销量的商品。

我已经为此工作了一周,这就是我所拥有的

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <algorithm>

using namespace std;

struct Sales
{
  string prodName;
  double proNum;
  string price;
  string sold;
};

void sortArray(struct database, int);   //Function prototypes
void sortString(string[], int);
void showArray(struct database[], int);
bool sales_sorter(Sales const& lhs, Sales const& rhs);

int main()
{
  ifstream fin;

  fin.open("sales.txt");

  if (fin.fail())
   {
      cout << "Failed to open file" << endl;
   }


  vector<Sales> database(5);
  string line;

  int i = 0;
  while (!fin.eof())
  {
    for (int j = 0; j < 5; j++) 
    {
        if (j == 0) // name
        {
            fin >>  database[i].proNum;
        }
        else if (j == 1) // 
        {
            fin >> database[i].prodName;
        }
        else if (j == 2)
        {
            fin >> database[i].price;
        }
        else if (j == 3)
        {
            fin >> database[i].sold;
        }
    }
    i++; //move to next item
  }
}

std::sort(sales.begin(), sales.end(), &sales_sorter);

cout << &sales_sorter;


/* for (int x = 0; x < 5; x++)   //Just used to make sure the array is working 
{
    cout << database[x].proNum << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].prodName << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].price << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].sold << endl;
} */
system("pause");


   return 0;
}

void sortArray(double database[], int)
{
  bool swap;
  int temp;

  do
  {
    swap = false;
    for (int count = 0; count < (5 - 1); count++)
    {
        if (database[count] > database[count + 1])
        {
            temp = database[count];
            database[count] = database[count + 1];
            database[count + 1] = temp;
            swap = true;
        }
    }
  } while (swap);
}


void showArray(double database[], int)
{
  for (int count = 0; count < 5; count++)
    cout << database[count] << " ";
  cout << endl;
}

bool sales_sorter(Sales const& lhs, Sales const& rhs)
{
   if (lhs.prodName != rhs.prodName)
    return lhs.prodName < rhs.prodName;
   if (lhs.proNum != rhs.proNum)
    return lhs.proNum < rhs.proNum;
   if (lhs.price != rhs.price)
    return lhs.price < rhs.price;
   return lhs.sold < rhs.sold;
 }

现在我从thread 中得到了排序的想法,但是我的排序布尔抛出了这个错误:“错误应该是一个“;”但是没有地方可以放置它而不破坏东西,有人可以帮我弄清楚如何排序。我经历了很多不同的线程,但所有这些项目都需要排序,我似乎找不到任何关于指向数组的数据结构!

【问题讨论】:

  • 您正在尝试在main 中定义函数sales_sorter。你不能那样做。
  • @molbdnilo 谢谢!我不知道这个

标签: c++ arrays sorting


【解决方案1】:

您的代码至少存在几个问题。 首先,您不能在其他函数中定义函数,因此您应该将 sales_sorter 函数从 main 中取出。其次,您有泄漏-您分配了database,但您从未解除分配它。我会用std::arraystd::vector 替换它。例如,而不是

 Sales *database = new Sales[5];

你将拥有

 std::array<Sales, 5> database;

 std::vector<Sales> database(5);

然后你需要将你的容器传递给排序函数:

 std::sort(database.begin(), database.end(), &sales_sorter);

这应该可以帮助您入门。

【讨论】:

  • 您好,感谢您的帮助!这很有帮助,但是我收到一个错误,上面写着'sales_sorter': undeclared identifier。你知道它为什么会这样说吗?我已经更新了我上面的代码以反映你所说的
  • @DestryAmiott 好吧,您没有通过在 main 之前添加原型来转发声明它。
  • 谢谢 我不知道你可以像void 那样声明一个布尔原型。感谢您的帮助,我会将其标记为正确,但我仍然遇到错误,因此我会尝试解决。它给了我一个不在文件中的奇数,但感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
相关资源
最近更新 更多