对指向数组c ++的数据结构进行排序

时间:2015-10-10 16:15:39

标签: c++ arrays sorting

好的,我现在正在研究这个项目。该程序读入一个文件,该文件可以包含任意数量的行,文件中的每个项目都是不同的类型,如下所示:

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

第一个数字是产品编号是double类型,第二个是类型字符串,然后是每个价格的浮点数,然后是销售数字的int。程序读取这些然后对它们进行排序并输出最高销售和最低销售项目。

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

#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得到了排序的想法但是我的排序bool抛出了这个错误:&#34;错误预期a&#34 ;;&#34;但没有什么地方可以把它放在没有破坏的东西,有人可以帮我弄清楚如何排序这个。我经历了很多不同的线程,但所有这些项目都需要排序,我似乎无法找到任何指向数组的数据结构!

1 个答案:

答案 0 :(得分:3)

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

 Sales *database = new Sales[5];

你会有

 std::array<Sales, 5> database;

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

然后你需要将容器传递给sort函数:

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

这应该让你开始。