15

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(). but after user input i would like to sort the items according to the x value point.getX(). How i do this ?

It seems like set container does not have a sort function. 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

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.

M.A
  • 1,073
  • 2
  • 15
  • 21

3 Answers3

19

You cannot resort a set, how it sorts is part of the type of the particular set. A given set has a fixed set order that cannot be changed.

You could create a new set with the same data relatively easily. Just create a new set that sorts based on the new criteria.

If you want to use the two sets in the same code, you'll have to abstract the access to the underlying set.

Now, if you are doing rare reads and modifications, using a vector that you sort manually is often a better idea. You can remove duplicates by using the std::unique-erase idiom.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Vector is ok with me. but i am not sure how to implement the std::unique-erase . Can u advise ? – M.A Nov 14 '12 at 19:07
  • 2
    @user1571494: Given a sorted vector, `v.erase(std::unique(v.begin(), v.end()), v.end());` will remove duplicates. – Mike Seymour Nov 14 '12 at 19:09
  • 3
    Note that the above code @Mike wrote will use `operator==` to compare them. You may need to pass in a predicate so that you only nuke things that are sort-equivalent `(!(a – Yakk - Adam Nevraumont Nov 14 '12 at 19:16
  • Also note that don't miss that last `, v.end()` -- it will compile, but won't do anything sensible, but may pass a few manual tests. – Yakk - Adam Nevraumont Jan 08 '21 at 17:34
10

std::set stores its members in a sorted fashion. If you walk through the set from .begin() to .end(), you will have a sorted list of items.

If you don't like the default sort criteria, you may supply a 2nd template parameter to std::set<>

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Yup.. I understand. but can i re-sort it – M.A Nov 14 '12 at 18:55
  • @user1571494 - No, you cannot re-sort it. You *can* copy the items out of that `std::set<>` and into a different container -- perhaps a `std::set` with a different comparison template parameter. – Robᵩ Nov 14 '12 at 18:57
  • I believe sets accept a comparator, or an object (either to a pointer to a function or a class with a function call operator) that is capable of taking 2 objects in the set and determining an absolute order between them. If you look at the example on the bottom of this page: http://www.cplusplus.com/reference/stl/set/set/, there is an example of a set with a custom comparator. – Wug Nov 14 '12 at 18:57
4

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

ByB: 3,6 2,7 1,8

JayS
  • 2,057
  • 24
  • 16