0

I'm trying to sort

std::vector< std::vector< std::string> > perm;

I am attempting to use std::sort from the algorithm header

I called it with

std::sort( perm.begin(), perm.end(), sortPerms);

This is my sort function:

bool sortPerms (const std::vector<std::string> &i, const std::vector<std::string> &j) {
  for(unsigned int x = 0; x < i.size(); x++) {
    if(i[x] != j[x])
      return false;
  }
  //both are equal
  return true;
}

The purpose of sorting is to then call std::unique to obtain a vector with unique values. When I compile with gcc in cygwin, I get no errors but I have repeats, and when I compile with visual studio 2010, I get an error that operator< is not defined. I stepped through and it is attempting to use its own sort function, and not the one that I have defined.

I'm not sure how to fix this, any suggestions?

Other details: It is guaranteed that all vectors will be of same size. It's purpose is a vector of every permutation of an original vector of strings. Each string is a command, and I am looking for all of the different ways these commands can be shuffled. So I need to strip the duplicates.

5
  • Can you point to the line in which you have the error, please? Commented Oct 25, 2012 at 0:27
  • 5
    How do you expect to sort something based solely on a concept of being unequal? Commented Oct 25, 2012 at 0:29
  • Its an assertion inside of <algorithm> that states that operator< is undefined. I'm not sure why that's an issue if I supplied my own comparator Commented Oct 25, 2012 at 0:33
  • I don't see supplied operator but a callback function to std::sort(). I guess on Windows it uses LEFT < RIGHT, where LEFT is in your case std::vector< std::string >. You may need to declare structure with an operator< inside that would derive from std::vector< std::string > and work through it. Commented Oct 25, 2012 at 0:36
  • 1
    @Grzegorz: Why a new type that derives std::vector<std::string>? Just make a free ("global") operator< like sane people! bool operator<(const std::vector<std::string>& a, const std::vector<std::string>& b); Commented Oct 25, 2012 at 0:37

1 Answer 1

3

Your sort function should return when a < b, not a != b.

Also, by default, std::vector<> should already support lexicographic comparison via operator<, which should do what you're expecting (compare each element in turn, with the first non-equivalent elements used for the comparison). See http://en.cppreference.com/w/cpp/container/vector/operator_cmp

Your current sortPerms function looks like what you would pass to std::unique, but that has different behavior than a predicate used for std::sort.

Sign up to request clarification or add additional context in comments.

1 Comment

sweet, thanks. I wasn't sure if vector would have a function of such, thanks. Also, I don't think that a < b, comparing the actual commands, would work in this case for grouping like std::vector<std::string>'s together. But I'm aware that's usually how its done. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.