0

I have a character array that I want to sort. The issue is if I pass the array as it is to the sort function than it actually converts the array characters which are actually numbers in their ASCII equivalents. For e.g. 4 become 52.

std::vector<int> classStudents;
....
char* cend = cAllowedStudents+maxAllowedStudents;
std::sort(cAllowedStudents, cend);
std::set_difference(classStudents.begin(), classStudents.end(),cAllowedStudents, cend,std::back_inserter(diff));

I also tried converting the whole array into separate int array through this, (however ideally I don’t want to use another array but only as a last choice):

iAllowedStudents[i]=(int)cAllowedStudents[i];

But it also does the same, so how can I convert this cAllowedStudents to be used with std::set_difference

10
  • 1
    what is the type of classStudents? Commented Jun 11, 2014 at 9:39
  • 1
    How about accepting one of the answers for the related question you posted on this yesterday? Commented Jun 11, 2014 at 9:40
  • 1
    How should comparing work during sorting? Is 'a' < '4' or 'a' > '4'? Commented Jun 11, 2014 at 9:42
  • 1
    Your question is not very clear, why would you compare an array of ints with an array of chars? What is the information in the character sequence that is of interest? Maybe if you added an example, that would be a little clearer... as it stands, voting to close.. Commented Jun 11, 2014 at 9:43
  • 1
    @Maven no, I see all your comments answered; actually you didn't answers comments on clarifying the question further, and you expect answers :) Commented Jun 11, 2014 at 9:56

1 Answer 1

1

Use:

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

and pass a custom comparison function

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

Comments