c++ 中具有超过 1 种排序方法的 STL 容器

发布于 2024-09-16 16:39:41 字数 352 浏览 3 评论 0原文

我正在寻找一个容器,用于包含 Employee 等对象(包含信息:姓名、工资、电话......) 例如,可以一次按姓名(a..z)排序,其他时间按工资排序。 最好的方法是什么? 我想到了地图,但后来我只定义了 1 个按键 我会欣赏每一个想法(请不要太先进!)

--- 更新 ---

我实际上不需要总是维护 2 个 STL 容器,我通常会拥有 1 个(比如按姓氏排序的员工),并且根据要求,我不介意创建一个新的STL容器,然后再次将所有元素推入其中,只是这次按工资排序,这样我就可以按该顺序打印它。是否可以创建带有名称排序的map1和带有工资排序的map2?如果是这样,希望得到进一步的解释\定义这两个地图的示例。我的 C++ 知识很少(我得到的第一个作业)

i am looking for a container, to contain objects like Employee (with info: name, salary, phone ....)
that will be possible to once sort it by name (a..z) and other time sort it by salary for example.
what is the best way to do it ?
i thought about map, but then i define only 1 key to go by
would appreciate every idea (not too advanced please ! )

--- update ---

I actually don't need to always maintain 2 STL containers, i would normally have 1 ( say Employees sorted by last name), and upon request, I don't mind making a new STL container, and pushing all the elements to it again, only this time to be sorted by salary, so i can print it by that order. Is it possible to create map1 with name sort, and map2 with salary sort ? if so would love further explanation \ example for defining these 2 maps. I have very little c++ knowledge ( first assignment i got )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画骨成沙 2024-09-23 16:39:41

使用此版本的 std::sort

template <class RandomAccessIterator, class Compare>
void sort( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

您可以对您想要的任何字段进行排序,并提供您自己的比较器。例如

struct CompareSalary
{
  bool operator () ( const Employee& a, const Employee& b ) const
  {
    return a.salary < b.salary;
  }
}

,由于 std::sort 与提供随机访问迭代器的所有容器兼容,因此 std::vector 就可以了。

using this version of std::sort

template <class RandomAccessIterator, class Compare>
void sort( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

you can sort on whatever field(s) you want, supplying your own comparator. For instance

struct CompareSalary
{
  bool operator () ( const Employee& a, const Employee& b ) const
  {
    return a.salary < b.salary;
  }
}

Also since std::sort is compatible with all containers providing a random acess iterator, std::vector will do just fine.

入画浅相思 2024-09-23 16:39:41

如果您希望同时使用两个排序标准,您还可以查看 Boost MultiIndex

Ps:但是既然你提到你是c++新手,我不建议使用Boost MultiIndex。很难理解它的语法

If you want both the sorting criteria to be available at same time you could also look into Boost MultiIndex

Ps: But since you mentioned that you are new to c++ i would not recommend using Boost MultiIndex. Its difficult to understand its syntax

呢古 2024-09-23 16:39:41

std::sort 提供一个函数:

bool byName(Employee left, Employee right) {
    return left.name < right.name;
}

std::vector<Employee> employees;
std::sort(employees.begin(), employees.end(), byName);

Provide a functional to std::sort:

bool byName(Employee left, Employee right) {
    return left.name < right.name;
}

std::vector<Employee> employees;
std::sort(employees.begin(), employees.end(), byName);
未央 2024-09-23 16:39:41

基本上,您需要定义多个比较器,每个比较器都实现以满足不同的排序标准。 Philip Potter 的帖子给出了一个排序标准的示例。您可能还想定义更多这样的内容。

重载小于运算符将使您能够仅使用前两个参数的 std::sort 方法,但您将仅限于一个排序标准。

Basically you would want to define multiple comparator, each implemented to fulfill the different sorting criteria. The post by Philip Potter gives an example of one sorting criteria. You may want to define few more like this.

Overloading the less than operator will enable you to use the std::sort method with the first two parameters only, but you would be limited to just one sorting criteria.

浅唱ヾ落雨殇 2024-09-23 16:39:41

您希望它们始终被排序吗?就像 std::map 一样吗? (所以你可以使用 *(coll.begin()) 访问最低元素?)如果是这样,我要做的是有两个 std::map,每个都充满 shared_ptr; 的,每个函数都传递自己的排序函子,每个排序标准一个。这样,您的数据类型就不再局限于单个“小于”运算符,您可以获得 O(log n) 插入和删除(std::map 只是一棵二叉树),并且映射始终是排序的。

但是,您必须同步添加和删除,以确保它们在两个地图中添加和删除。

Do you want them to be sorted at all times? Like a std::map does? (So you can access the lowest element with *(coll.begin())?) If so, what I would do is have two std::map's, each full of shared_ptr<T>'s, each of which is passed its own sorting functor, one for each sorting criteria. This way, you're not limited to a single "less-than" operator for your datatype, you get O(log n) inserts and deletes (std::map is just a binary tree), and the maps are is always sorted.

You'd have to synchronize the adding and removing to make sure they're added and removed from both maps, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文