如何通过私有字符串变量按字母顺序对对象向量进行排序 [英] How to sort vector of objects by a private string variable alphabetically

查看:95
本文介绍了如何通过私有字符串变量按字母顺序对对象向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何通过称为VIN的成员变量之一对对象向量进行排序,该成员变量的数据类型为string.我要执行冒泡排序或选择排序.我对气泡排序最熟悉,所以这就是我试图采取的方向.但是,我知道我需要通过将它们全部变为大写或小写来比较两个字符串.请记住,字符串中也将包含数字字符.

I cannot figure out how to sort a vector of objects by one of the member variables called VIN, which is of data type string. I am to either perform a bubble sort or a selection sort. I am most familiar with a bubble sort, so that's the direction I attempted to take. However, I am aware that I need to compare both of the strings by either making them all uppercase or lower case. Keep in mind, the strings also will have number characters in them as well.

所以我的问题是: 1)如何转换所有小写或所有ASCII数字的字符串,以及 2)temp变量应为哪种数据类型(类的数据类型或其他某种数据类型).

So my question is: 1) How to I convert a string either all lowercase or all ASCII numbers and 2) What data type should the temp variable be (data type of the class or some other data type).

这是我到目前为止拥有的(不完整的)代码:

Here is the (incomplete) code I have so far:

    void sortInventory(vector<Vehicle> &carList)
    {
        bool swap; 
        string temp; 
        do
        {
            swap = false;

            for (int count = 0; count < carList.size(); count++)
            {
                if (carList[count].getVIN() > carList[count + 1].getVIN())
                {
                    temp = carList[count].getVIN();                                 
                    carList[count].getVIN() = carList[count + 1].getVIN();          
                    carList[count + 1].getVIN() = temp;                      
                    swap = true;                                               
                }
            }

        } while (swap);
}

这是我的班级声明:

class Vehicle
{
private:
    string VIN;

public:
    string getVIN();
    void   setVIN(string);

};

这是我的课程实现:

string Vehicle::getVIN()
{   return VIN;     }

void Vehicle::setVIN(string input)
{   VIN = input;    }

谢谢!

推荐答案

您可以执行std::sort(carList.begin(),carList.end(),vehicleCompare),其中vehicleCompare是您定义的比较函数.请参阅排序文档.然后,可以大写使用 std :: toupper ,如图所示在本指南中.

You can do std::sort(carList.begin(),carList.end(),vehicleCompare) where vehicleCompare is a comparison function that you define. See sort documentation. Then, to uppercase you can use std::toupper, as shown in this guide.

std::string myToUpper(std::string in) {
    std::transform(in.begin(), in.end(),in.begin(), ::toupper);
    return in;
}

因此比较运算符(*)将为:

bool vehicleCompare (const Vehicle a, const Vehicle b) {
    const std::string a_name = myToUpper(a.getVIN());
    const std::string b_name = myToupper(b.getVIN());
    return (a_name < b_name);
    }

关于字符串比较运算符的有用读物.

顺便说一句,您的string getVIN()方法应为const,即您应将其声明更改为string getVIN() const.

By the way, your string getVIN() method should be const, that is you should change its declaration to string getVIN() const.

如果要保留排序功能,关键是无论如何都必须定义一个适当的比较运算符,如此处所示.

If you want to keep your sorting function, the point is that in any case you'll have to define a proper comparison operator, as the one shown here.

要专门回答第二个问题,在c ++ 11中,temp可以是auto,也可以是std::string.那么您尝试分配VIN值的方法是错误的.给定您给定的接口,它应该是:

To specifically answer to your second question, temp could be auto in C++11, or simply std::string. Then the way you are trying to assign your VIN value is wrong. Given the interface you have given, it should be:

auto temp = carList[count].getVIN();                                 
carList[count].setVIN(carList[count + 1].getVIN() );          
carList[count + 1].setVIN(temp);

尽管当您开始复制多个成员变量时,它仍然可能令人讨厌:您应该构建一个复制构造函数并将代码更改为:

Although it still might get nasty when you start to have more than one member variable to copy: you should instead build a copy constructor and change your code to:

auto temp = carList[count]; //you can use Vehicle instead of auto as type
carList[count] = carList[count + 1];          
carList[count + 1] = temp;

副本构造函数将为:

Vehicle(const Vehicle& in) 
  : VIN(in.getVIN() ) 
  {}

而且,到那时,您还需要一个来自字符串的构造函数和一个空的构造函数.

And, at that point, you'll also want a constructor from string, and an empty constructor.

Vehicle(std::string& inVIN) 
  : VIN(inVIN) 
  {}

Vehicle(const Vehicle& in) 
  {} //All input members get initialized through the default constructor.


(*)请注意,这种比较方法不是最有效的,因为它使整个字符串都大写,而前几个字符通常足以确定其顺序.因此,一种更有效的方法是一次将一个字符大写,然后在决定是否将另一个字符大写之前进行比较.

这篇关于如何通过私有字符串变量按字母顺序对对象向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆