calccrypto
calccrypto

Reputation: 8991

how to sort a vector of pairs?

I am trying to sort a vector of pair<int, T> (where T is the templated value type of the class) and codeblocks is giving me massive amounts of errors i dont understand why. is there there some special syntax required to sort vector<pair<int, T> >? i did make the comparison function, which did not use T at all

here's the code

  bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1] -> first;
    int x = 0;
    int current_level = order[x] -> first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x] -> first == current_level){
            cout << order[x] -> second << " ";
            x++;
        }
    }
  }

Upvotes: 1

Views: 6937

Answers (5)

Guy Sirton
Guy Sirton

Reputation: 8401

The posted code didn't compile but when I was trying to make it compile I noticed you probably want:

order.push_back(std::make_pair(level, root -> value));

Also:

int max_level = order[order.size() - 1]. first;

This fixed version of yours compiles for me:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class T
{};

template <class T> class Node
{
public:
T value;
Node<T>* left;
Node<T>* right;
};

Node<T>* root_;

bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(std::make_pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1]. first;
    int x = 0;
    int current_level = order[x].first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x]. first == current_level){
//            cout << order[x]. second << " ";
            x++;
        }
    }
  }

This part came before the full code was posted but may still be useful to someone trying to figure out sort so I'll keep it in: Generally for sorting you may need to provide a way of comparing the pairs, e.g. see here: http://www.cplusplus.com/reference/algorithm/sort/

If you use the sort algorithm it will work automagically for anything that has the less than operator defined but not for other types.

std::pair should provide a default less than operator so perhaps there is another issue - can we see the code? As Tomalak notes, that is probably because you have no way of comparing T's.

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254431

It looks like these are member functions of a class template. If that is the case, then sortlevel will need to be static (or a non-member) in order to be used as the comparator in std::sort().

Also, you've written e.g. order[x]->first in a few places, when it should be order[x].first.

Upvotes: 1

shoosh
shoosh

Reputation: 78914

Whenever you're using T you need to make sure it is in a class or a function with a template<typename T> before it.

in your code:

  • you need template<typename T> before the definition of sortlevel, get_level and level_order
  • when calling sort you need to classify sortlevel<T>.
  • when calling these function, call them with the actual type.

Upvotes: 0

shoosh
shoosh

Reputation: 78914

Provide your own comparison function (or functor) as the last argument to sort. Your comparison should take the first of the pair and compare it.

for instance:

template<typename T>
bool myfunction (const pair<int, T>& i, const pair<int, T>& j)
{ 
   return (i.first < j.first); 
}

sort (myvector.begin(), myvector.end(), myfunction<ActualType>);

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

If you start with:

#include <vector>
#include <algorithm>

struct T {
   T(int x) : x(x) {};

   int x;
};

int main() {
   std::vector<std::pair<int, T> > v;

   std::pair<int, T> a = std::make_pair(0, T(42));
   std::pair<int, T> b = std::make_pair(1, T(36));

   v.push_back(a);
   v.push_back(b);

   std::sort(v.begin(), v.end());
}

Add a comparator for T, that std::pair<int, T>'s default-generated comparator will invoke:

struct T {
   T(int x) : x(x) {};
   bool operator<(const T& other) const {
      return x < other.x;
   }

   int x;
};

Upvotes: 1

Related Questions