Всем привет, есть такой код, считывающий массив и сортирующий слова в нём в порядке возрастания:
#include <bits/stdc++.h>
using namespace std;
// Function to check the Vowel
bool isVowel(char ch)
{
ch = toupper(ch);
return (ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O'
|| ch == 'U');
}
// Returns count of vowels in str
int countVowels(string str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
if (isVowel(str[i])) // Check for vowel
++count;
return count;
}
// Function to sort the array according to
// the number of the vowels
void sortArr(string arr[], int n)
{
// Vector to store the number of vowels
// with respective elements
vector<pair<int, string> > vp;
// Inserting number of vowels
// with respective strings
// in the vector pair
for (int i = 0; i < n; i++) {
vp.push_back(
make_pair(
countVowels(
arr[i]),
arr[i]));
}
// Sort the vector, this will sort the pair
// according to the number of vowels
sort(vp.begin(), vp.end());
// Print the sorted vector content
for (int i = 0; i < vp.size(); i++)
cout << vp[i].second << " ";
}
// Driver code
int main()
{
string arr[] = { "lmno", "pqrst",
"aeiou", "xyz" };
int n = sizeof(arr) / sizeof(arr[0]);
sortArr(arr, n);
return 0;
}
К сожалению, преподаватель запрещает использовать векторы и классы :( Подскажите, как переделать без них?
запрещает использовать классы
. Он точно вам не Си преподаёт?sort
- естьqsort
...