【问题标题】:Sorting a vector with pointer at object C++用对象 C++ 处的指针对向量进行排序
【发布时间】:2020-09-17 07:13:19
【问题描述】:

如果有人能提供帮助,我将不胜感激。 我如何对这个向量进行排序:

vector<Person*>person

按照这个标准:

Surname

我已经尝试过使用 set 但如果有超过 2 个具有相同 Surname 的对象,它会删除对象

字符串变量很多,需要排序

Surname

然后如果姓氏相同,那么我需要对它们进行排序

Name

它还按该指针的十六进制值排序... 编辑: 你问的更多代码:

for (pChild = pRoot->FirstChildElement("Member"); pChild != NULL; pChild = pChild->NextSiblingElement())
    {
        string Surname = pChild->Attribute("surname");
        string Name = pChild->Attribute("name");
        string DateOfBirth = pChild->Attribute("dateofbirth");
        person.push_back(new Person(Surname, Name, DateOfBirth));
    }

【问题讨论】:

  • 如果 set 排序正确,您可以使用 multy_set
  • Set 确实对它进行了正确排序,但只有十六进制值,而不是字符串变量...
  • 您可以使用比较器。看到这个:stackoverflow.com/questions/1380463/…
  • @NikolaStjepanović 创建一个比较器函数,该函数接受指向对象的指针并根据指向对象的内容返回结果。

标签: c++ sorting pointers


【解决方案1】:

如果不显示更多代码,很难为您提供帮助,但我会查看 std::sort() 的文档,因为您可以创建自定义运算符来对向量进行排序。

【讨论】:

  • 我有对象向量,但对象上有指针。所以我需要排序的是整个向量,所以该向量排序的所有成员都是通过这个变量:m_sSurname(我已经将字符串和int变量存储到对象中,然后该对象存储在该向量中)
  • @NikolaStjepanović 你读过文档吗?您只是在重复您在问题中已经说过的话。特别注意第三个参数comp
  • 添加了更多示例,我将哪些信息存储到向量中。
  • 这个答案正是下面仅以文字形式发布的内容。
  • @BaileyKocin 说句公道话,当这个问题第一次发布时,我几乎没有什么信息可以发布。
【解决方案2】:

这是一个完整的例子

#include <vector>
#include <iostream>
#include <algorithm>

class Person
{
public:
    std::string s1, s2, s3;

    Person(std::string S1, std::string S2, std::string S3) : s1(S1), s2(S2), s3(S3) {}
};

struct less_than_key
{
    inline bool operator() (const Person* const p1, const Person* const p2)
    {
        if (p1->s1 < p2->s1)
            return true;
        else if (p1->s1 == p2->s1 && p1->s2 < p2->s2)
            return true;

        return false;
    }
};

int main()
{
    std::vector<Person*> persons{ new Person("C", "D", "E"), new Person("C", "C", "D"),
                                  new Person("B", "C", "D"), new Person("B", "C", "E")};

    std::sort(persons.begin(), persons.end(), less_than_key());

    for (auto person : persons)
    {
        std::cout << person->s1 << ' ' << person->s2 << std::endl;
    }

    return 0;
}

【讨论】:

  • 非常感谢,经过这么多的努力终于成功了
【解决方案3】:

我在使用std::set 时玩得很开心。有几个比较器的例子。一个函数和一个“函子”。

#include <iostream>
#include <set>
#include <string>

struct Person {
  uint64_t id;
  std::string name;
  std::string family_name;

  bool operator<(const Person &other) const {
    if (family_name == other.family_name) {
      if (name == other.name) {
        return id < other.id;
      } else {
        return name < other.name;
      }
    } else {
      return family_name < other.family_name;
    }
  }
};

std::ostream &operator<<(std::ostream &os, const Person &x) {
  return os << '{' << x.id << ", " << x.name << ", " << x.family_name << '}';
}

bool person_ptr_less(const Person *a, const Person *b) { return *a < *b; }

class PersonPtrComparator {
public:
  bool operator()(const Person *a, const Person *b) const { return *a < *b; }
};

int main() {
  std::set<Person *, bool (*)(const Person *, const Person *)> people(
      person_ptr_less);
  people.emplace(new Person{1, "Joe", "Smith"});
  people.emplace(new Person{2, "Joe", "Blow"});
  people.emplace(new Person{3, "Joa", "Smith"});
  people.emplace(new Person{4, "Joe", "Smith"});

  std::set<Person *, PersonPtrComparator> people_2(people.begin(),
                                                   people.end());

  for (const auto &x : people) {
    std::cout << *x << '\n';
  }
  std::cout << "---\n";
  for (const auto &x : people_2) {
    std::cout << *x << '\n';
  }
  return 0;
}

【讨论】:

  • 谢谢,我在我的示例中尝试了此代码,它也和最后一个答案一样有效。
【解决方案4】:

你可以像这样使用比较器:

// Simple class
class Person {
    public:
     string name;
    Person(string name) {
      this->name = name;
    }
};

// create a comparator like this with two objects as parameters.
bool comparator(Person* a, Person *b) {
  return a->name > b->name;
}

int main() {
  vector<Person* > v;
  v.push_back(new Person("ajay"));
  v.push_back(new Person("tanya"));

  // pass the comparator created into sort function.
  sort(v.begin(), v.end(),comparator);

  // printing output to check
  for(int i=0;i<v.size();i++) {
    cout<<v[i]->name<<endl;
  }
}

【讨论】:

  • 请不要在不需要的情况下包含 bits/std++.h。该操作使用 3 个标头。它们不应该包括所有那些预编译的。
猜你喜欢
  • 2014-03-11
  • 2021-09-20
  • 1970-01-01
  • 2014-12-29
  • 2017-09-14
  • 2022-01-24
  • 2016-07-06
  • 1970-01-01
  • 2018-06-07
相关资源
最近更新 更多