#include "widget.h"#include "ui_widget.h"#include <stdlib.h>#include <QDateTime>#include <QDebug>static int cmp(const void *a, const void *b){ return strcmp(* (char * const *) a, * (char * const *) b);}Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); QString str; char arr[20]; qsrand(QDateTime::currentMSecsSinceEpoch()); for(int i=0; i<20; i++) { arr[i] = qrand()%100 + 1; str.append(QString::number(arr[i]) + " "); } qDebug()<<str; qsort(&arr[0], 20, sizeof(char *), cmp); str.clear(); for(int i=0; i<20; i++) { str.append(QString::number(arr[i]) + " "); } qDebug()<<str;}Widget::~Widget(){ delete ui;}
static int cmp(const void *a, const void *b){ return strcmp((const char*) a, (const char*) b);}
C++ (Qt)static int cmp(const void *a, const void *b){ char c1 = *(char *) a; char c2 = *(char *) b; if (c1 < c2) return -1; if (c1 > c2) return +1; return 0;}
C++ (Qt)qsort(&arr[0], 20, sizeof(char), cmp);
qsort(&arr[0], 20, sizeof(char *), cmp);
qsort(&arr[0], 20, sizeof(char), cmp);
std::sort(&arr[0],&arr[20]);
C++ (Qt)#include <iostream>#include <algorithm>#include <random>#include <iterator> using namespace std; int main(){ const int N = 20; char arr[N]; std::mt19937 gen; std::uniform_int_distribution<int> dist(0, 100); std::generate(arr, arr+N, std::bind(dist, gen)); std::cout << "before sorting:" << std::endl; std::copy(arr, arr+N, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl << "after sorting:" << std::endl; std::sort(arr, arr+N); std::copy(arr, arr+N, std::ostream_iterator<int>(std::cout, " ")); return 0;}
C++ (Qt)#include <iostream>#include <algorithm>#include <random>#include <iterator>#include <vector> using namespace std; int main(){ const int N = 20; std::vector<char> arr(N); std::mt19937 gen; std::uniform_int_distribution<int> dist(0, 100); std::generate(arr.begin(), arr.end(), std::bind(dist, gen)); std::cout << "before sorting:" << std::endl; std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl << "after sorting:" << std::endl; std::sort(arr.begin(), arr.end(),[](char a, char b){return a<b;}); std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); return 0;}
#include <iostream>#include <algorithm>#include <random>#include <iterator>#include <vector> using namespace std; int main(){ const int N = 20; std::vector<char> arr(N); std::mt19937 gen; std::uniform_int_distribution<int> dist(0, 100); std::generate(arr.begin(), arr.end(), std::bind(dist, gen)); std::cout << "before sorting:" << std::endl; std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl << "after sorting:" << std::endl; std::sort(arr.begin(), arr.end(),[](char a, char b){return a<b;}); std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); return 0;}
C++ (Qt)gen.seed(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));