C ++插入对矢量进行排序

时间:2013-04-25 17:24:24

标签: c++ vector

我正在尝试对我昨天在前一篇文章的帮助下创建的棒球投手向量进行插入排序。我想通过ERA1按升序对投手进行排序。我已经获得了插入排序,以便在过去使用一组整数。我想我的代码中有一个语法错误,用于插入排序。直到尝试添加插入排序,这个程序运行良好。我收到一个错误 - 在[令牌]之前预期不合格的ID。提前感谢您的帮助。

#ifndef Pitcher_H
#define Pitcher_H
#include <string>
#include <vector>

using namespace std;

class Pitcher
{
private:
    string _name;
    double _ERA1;
    double _ERA2;

public:
    Pitcher();
    Pitcher(string, double, double);
    vector<Pitcher> Pitchers;
    string GetName();
    double GetERA1();
    double GetERA2();
    void InsertionSort(vector<Pitcher>&);
    ~Pitcher();

};

#endif

#include "Pitcher.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

Pitcher::Pitcher()
{
}

Pitcher::~Pitcher()
{
}

string Pitcher::GetName()
{
    return _name;
}

Pitcher::Pitcher(string name, double ERA1, double ERA2)
{
    _name = name;
    _ERA1 = ERA1;
    _ERA2 = ERA2;
}

double Pitcher::GetERA1()
{
    return _ERA1;
}

double Pitcher::GetERA2()
{
    return _ERA2;
}


#include "Pitcher.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

void InsertionSort(vector<Pitcher> Pitchers&);

using namespace std;

int main()
{

    vector<Pitcher> Pitchers;

    cout  << "Pitcher" << setw(19) << "Item ERA1" << setw(13) << 
        "Item ERA2\n" << endl;

    Pitcher h2("Bob Jones", 1.32, 3.49); 
    Pitchers.push_back(h2); 
    Pitcher h3("F Mason", 7.34, 2.07); 
    Pitchers.push_back(h3); 
    Pitcher h1("RA Dice", 0.98, 6.44); 
    Pitchers.push_back(h1); 

    for(unsigned i = 0; i < Pitchers.size(); ++i)
    {
        cout << setw(19);
        cout << left << Pitchers[i].GetName() << "$" << 
            setw(10) << Pitchers[i].GetERA1() << 
            right << "$" << Pitchers[i].GetERA2() << "\n";
    }

    cout << endl;

//------------------------------------------------------

    InsertionSort(Pitchers);

//Now print the numbers
    cout<<"The numbers in the vector after the sort are:"<<endl;
    for(int i = 0; i < Pitchers.size(); i++)
    {
        cout<<Pitchers[i].GetERA1()<<" ";
    }
    cout<<endl<<endl;

    system("PAUSE");
    return 0;
}

void InsertionSort(vector<Pitcher> &Pitchers)
{
    int firstOutOfOrder = 0;
    int location = 0;
    int temp;
    int totalComparisons = 0; //debug purposes

    for(firstOutOfOrder = 1; firstOutOfOrder < Pitchers.size() ; firstOutOfOrder++)
    {
        if(Pitcher.GetERA1([firstOutOfOrder]) < Pitcher.GetERA1[firstOutOfOrder - 1])
        {
            temp = Pitcher[firstOutOfOrder];
            location = firstOutOfOrder;
            do
            {
                totalComparisons++;

                Pitcher.GetERA1[location] = Pitcher.GetERA1[location - 1];
                location--;
            }while(location > 0 && Pitcher.GetERA1[location - 1] > temp);
            Pitcher.GetERA1[location] = temp;
        }
    }
    cout<<endl<<endl<<"Comparisons: "<<totalComparisons<<endl<<endl;
}

2 个答案:

答案 0 :(得分:1)

下面:

  for(firstOutOfOrder = 1; firstOutOfOrder < Pitchers.size() ; firstOutOfOrder++)
{
    if(Pitchers[firstOutOfOrder].GetERA1() < Pitchers[firstOutOfOrder-1].GetERA1())
    {    //^^^your way was not right, should first access the object then 
         //access member function
        temp = Pitcher[firstOutOfOrder];
                 //^^^should be Pitchers, similar errors below           
        location = firstOutOfOrder;
        do
        {
            totalComparisons++;

            Pitcher.GetERA1[location] = Pitcher.GetERA1[location - 1];
           //^^^similar error as inside if condition
            location--;
        }while(location > 0 && Pitcher.GetERA1[location - 1] > temp);
                             //^^^similar error as inside if condition
        Pitcher.GetERA1[location] = temp;
        //^^similar error as in if condition and name error
    }
}

同时,您将InsertionSort声明作为Pitcher

的成员
  public:
       .
       .
      void InsertionSort(vector<Pitcher>&);

并且您还在main

中声明了相同的功能
  void InsertionSort(vector<Pitcher> Pitchers&);
                        //should be vector<Pitcher>& Pitchers
  using namespace std;
  int main()

在您的情况下,可能应删除成员函数。 InsertionSort不是responsibility班级的Pitcher

答案 1 :(得分:0)

除非这是家庭作业,否则最好使用

中的排序构建
<algorithm>