ask for an algorithm for mixed sorting of numbers and characters in CPP.
input is a string array, all elements are essentially string, such as [2, Banana, 1, Apple, 3, Pear] . Now sort it, requiring that the sorted output be [1, Apple, 2, Banana, 3, Pear] . In short, it is the sorting of numbers and numbers, the sorting of characters and characters, it turns out that the position of string is the position of string, and the position of integer is integer (in other words, all elements are essentially string, but some of them are in digital form).
I think it is necessary to determine whether a string is a number or not. CPP should be able to use:: isdigit directly, right? But excuse me, how should I implement where string is originally string and integer where integer is ?
maybe you can use heap sorting to implement this algorithm. So ask another question about the definition of the minimum heap priority_queue.
struct cmp
{
bool operator () (int a, int b)
{ return a>b; }
};
void heaping1()
{
priority_queue<int, vector<int>, cmp> pq;
the definition of priority_queue in this way can be compiled. However, if you change to the following definition:
void heaping2()
{
priority_queue<pair<int, char>, cmp> pq;
in this way, the definition of priority_queue cannot be compiled! Of course, if you delete the cmp and change it to the maximum heap, then everything will be fine. So, how should the minimum heap in the above function be defined? where should I put the cmp? Thank you first!