插入排序向量

C++ Insertion Sort a vector

本文关键字:向量 插入排序      更新时间:2023-10-16

我正在尝试对昨天在上一篇文章的帮助下创建的棒球投手向量进行插入排序。我想按ERA1升序对投手进行排序。我已经让插入排序在过去对一组整数起作用了。我想我在插入排序的代码中有一个语法错误。在尝试添加插入排序之前,这个程序运行得很好。我在[token]前得到一个错误预期的不合格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 ERA2n" << 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;
}

此处:

  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不是你的Pitcher类的responsibility

除非这是作业,否则您最好使用from

的内置排序。
<algorithm>