按照c ++中的字母顺序排序对象的向量 [英] Sorting a vector of objects alphabetically in c++
问题描述
所以我创建了一个包含产品对象的向量。
产品具有int ID,字符串制造商和字符串productname。
我们已经存储了一些产品。
So I've created a vector which contains product objects. The product has a int ID, string manufacturer and string productname. Lets say I've stored a few products by doing this
vector<product*>productlist;
Product*p = new Product(123, "Sony", "C vaio laptop")
Product*p1 = new Product(1234, "LG", "D 54 inch TV")
Product*p2 = new Product(1235, "Lays", "A potato chips")
productlist.push_back(p);
productlist.push_back(p1);
productlist.push_back(p2);
我有一个名为getproductname(){return productname;}的方法可以用来获取产品名称,我可以排序产品名称,但之后,我不知道如何继续,因为我不知道如何打印整个对象按字母顺序的产品名称。
I have a method called getproductname(){return productname;} which can be used to get the productname, and I can sort the productnames, but after that I have no idea how to continue as I don't know how to print the whole object by alphabetically of their productname.
现在,我想按照其产品名称的字母顺序对3个产品进行排序/打印。我该怎么做(排序部分)?
示例输出:
Now I want to sort/print the 3 products by alphabetical order of their productname. How can I do that(the sorting part)? Sample output:
按字母顺序排序的产品
产品ID1:1235
Product ID1: 1235
产品制造商:Lays
Product manufacturer: Lays
产品名称:薯片//名称以A开头,因此是第一个输出
Product name:A potato chips //name starts with A so it's the first output
产品ID2:123
产品制造商:Sony
Product manufacturer: Sony
产品名称:C vaio笔记本电脑
Product name: C vaio laptop
产品ID3:1234
Product ID3: 1234
产品制造商:LG
产品名称:D 54 inch电视//名称以D开头,因此是最后
Product name: D 54 inch TV //name starts with D so it's the last
我试过插入sort(productlist.begin ),productlist.end());但它只适用于带有字符串而不是对象的向量。
I've tried inserting sort(productlist.begin(), productlist.end()); but it only works on vectors with string and not objects.
问题首先太含糊/简单。编辑!
question asked was too vague/simple at first. Edited!
推荐答案
使用 STL code>容器是定义一个比较器函数,告诉
std :: sort
算法如何比较元素, 。因此,我们需要定义一个小于关系,我们可以通过为 Product
类创建一个小于运算符像这样:
The way to make sorting happen using STL
containers is to define a comparator function that tells the std::sort
algorithm how to compare the elements to place them in order. So we need to define a less than relationship which we can do by creating a less than operator for our Product
class like this:
struct Product
{
int ID = 0;
std::string manuf;
std::string name;
Product(int ID, const std::string& manuf, const std::string& name)
: ID(ID), manuf(manuf), name(name) // initialize members here
{
}
// overloading the < operator enables functions
// like std::sort to compare Product objects to
// order them accordingly
bool operator<(const Product& p) const
{
return name < p.name; // order by name
}
};
现在,如果我们发送一个装满产品
对象 std :: sort
,它们将根据名称进行排序。
Now if we send a container full of Product
objects to std::sort
they will be sorted according to name.
但是, code>产品对象通过他们的指针,所以我们需要另一个小于operator :sort 函数,在使用我们的比较函数进行比较之前取消引用指针。
However, we need to sort Product
objects through their pointers so we need another less than operator to hand to the std::sort
function that dereferences the pointers before making the comparison using our comparator function.
// Function object to sort pointers
struct SortProductPointers
{
// overload the function call operator
bool operator()(const Product* lhs, const Product* rhs) const
{
// dereference the pointers to compare their targets
// using the Product class's operator<(...) function
return *lhs < *rhs;
}
};
现在我们有两个可以调用 std :: sort
算法:
Now we have those two we can call our std::sort
algorithm:
int main()
{
std::vector<Product*> products;
products.push_back(new Product(1, "Lays", "A potato chips"));
products.push_back(new Product(3, "LG", "D 54 inch TV"));
products.push_back(new Product(2, "Sony", "C vaio laptop"));
// std::sort takes a third parameter which is the
// comparator function object
std::sort(products.begin(), products.end(), SortProductPointers());
for(const auto* p: products)
std::cout << p->ID << ": " << p->manuf << ", " << p->name << '\n';
// Don't forget to delete all your Products
for(auto* p: products)
delete p;
}
这篇关于按照c ++中的字母顺序排序对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!