5.编写一个程序,完成N(N=10)个字符串的升序排列(要求使用string类型及字符串处理的相关操作或函数)。
时间: 2023-05-26 19:03:31 浏览: 78
数组与字符串.docx
5星 · 资源好评率100%
#include <iostream>
#include <string>
using namespace std;
void sort(string arr[], int n) {
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(arr[i] > arr[j]) {
string temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
const int N = 10;
string arr[N] = {"hello", "world", "apple", "banana", "cat", "dog", "elephant", "fish", "grape", "horse"};
sort(arr, N);
cout << "Sorted array:" << endl;
for(int i = 0; i < N; i++) {
cout << arr[i] << endl;
}
return 0;
}
阅读全文