#include <iostream> #include <iomanip> using namespace std; void sort(char **); int main() { int i; char *string[5]; cout << "输入5个字符串:" << endl; for (i = 0; i < 5; i++) { string[i] = new char[10]; cin >> *(string + i); } sort(string); for (i = 0; i < 5; i++) delete[] string[i]; } void sort(char **str) { int i = 0, j; char *p = 0; for (i = 0; i < 4; i++) { for (j = i + 1; j < 5; j++) { if (strcmp(*(str + i), *(str + j)) < 0) { p = *(str + i); *(str + i) = *(str + j); *(str + j) = p; } } } cout << "after sort the chars :" << endl; for (i = 0; i < 5; i++) { cout << *(str + i) << endl; } }
字符串排序;指针的指针运用;new 与 delete
最新推荐文章于 2021-08-10 18:07:00 发布