简体   繁体   English

使用std :: sort排序集

[英]Sorting Sets using std::sort

I would like to know if we can sort a pre created set. 我想知道我们是否可以对预先创建的集进行排序。 When I first create the set s_p2, I sort using a different element point.getLength(). 当我第一次创建set s_p2时,我使用不同的元素point.getLength()进行排序。 but after user input i would like to sort the items according to the x value point.getX(). 但是在用户输入之后我想根据x值point.getX()对项目进行排序。 How i do this ? 我怎么做的?

It seems like set container does not have a sort function. 似乎set container没有sort函数。 And i am advised to use vector. 我建议使用矢量。 But sets are able to store unique elements only. 但是集合只能存储唯一的元素。

Q1: How can i sort a set depending on the criteria Q1:我如何根据标准对集合进行排序

Q2: If set is unable to do this than which STL container is the best choice and how can i sort the elements in the container. Q2:如果set无法做到这一点,那么STL容器是最佳选择,如何对容器中的元素进行排序。

You cannot resort a set , how it sorts is part of the type of the particular set . 你不能诉诸一个set ,它如何排序是特定set类型的一部分。 A given set has a fixed set order that cannot be changed. 给定set具有无法更改的固定集顺序。

You could create a new set with the same data relatively easily. 您可以相对轻松地创建具有相同数据的新set Just create a new set that sorts based on the new criteria. 只需创建一个基于新标准进行排序的新set

If you want to use the two set s in the same code, you'll have to abstract the access to the underlying set . 如果要在同一代码中使用这两个set ,则必须抽象对底层set的访问。

Now, if you are doing rare reads and modifications, using a vector that you sort manually is often a better idea. 现在,如果您正在进行罕见的读取和修改,使用手动排序的vector通常是一个更好的主意。 You can remove duplicates by using the std::unique - erase idiom. 您可以使用std::unique - erase惯用法删除重复项。

std::set stores its members in a sorted fashion. std::set以排序的方式存储其成员。 If you walk through the set from .begin() to .end() , you will have a sorted list of items. 如果您从.begin().end()遍历集合,您将拥有一个已排序的项目列表。

If you don't like the default sort criteria, you may supply a 2nd template parameter to std::set<> 如果您不喜欢默认排序条件,则可以向std::set<>提供第二个模板参数

You can have two sets and keep them in sync or copy one to the other. 您可以拥有两个集合并使它们保持同步或将它们复制到另一个集合。

#include <iostream>
#include <set>

using namespace std;

struct AB
{
   AB(int a,int b) : _a(a),_b(b) {}

   int _a;
   int _b;
};

struct byA
{
    bool operator () (const AB& lhs, const AB& rhs)
    {
          return lhs._a <= rhs._a;
    }
}; 

struct byB
{
    bool operator () (const AB& lhs, const AB& rhs)
    {
       return lhs._b <= rhs._b;
    }
}; 

typedef set<AB,byA> ByA;
typedef set<AB,byB> ByB;
typedef ByA::const_iterator ByAIt;
typedef ByB::const_iterator ByBIt;

void getByB(const ByA &sA,ByB &sB)
{
   for(ByAIt iter=sA.begin(); iter!=sA.end();++iter) {
      const AB &ab=*iter;
      sB.insert(ab);
   }
}

int main(int argc, const char **argv)
{
   ByA sA;
   sA.insert(AB(3,6));
   sA.insert(AB(1,8));
   sA.insert(AB(2,7));

   ByB sB;
   getByB(sA,sB);

   cout << "ByA:" << endl;
   for(ByAIt iter=sA.begin(); iter!=sA.end();++iter) {
      const AB &ab=*iter;
      cout << ab._a << "," << ab._b << " ";
   }
   cout << endl << endl;

   cout << "ByB:" << endl;
   for(ByBIt iter=sB.begin(); iter!=sB.end();++iter) {
      const AB &ab=*iter;
      cout << ab._a << "," << ab._b << " ";
   }
   cout << endl;
   return 0;
}

program returns: ByA 1,8 2,7 3,6 程序返回:By A 1,8 2,7 3,6

ByB: 3,6 2,7 1,8 ByB:3,6 2,7 1,8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM