不要用 algorithm 和vector
时间: 2024-02-28 22:05:18 浏览: 19
好的,这是一个不使用 STL 的 C++ 代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
void sort_strings(string strings[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (strings[j] < strings[i]) {
swap(strings[i], strings[j]);
}
}
}
}
int main() {
string strings[10];
for (int i = 0; i < 10; i++) {
cin >> strings[i];
}
sort_strings(strings, 10);
for (int i = 0; i < 10; i++) {
cout << strings[i] << " ";
}
cout << endl;
return 0;
}
```
在这个实现中,我们定义了一个 `sort_strings` 函数,它接收一个字符串数组和字符串数组的长度作为输入,使用冒泡排序算法对字符串数组进行排序。
在主函数中,我们首先创建一个长度为 10 的字符串数组 `strings`,使用 `cin` 函数从标准输入读取 10 个等长的字符串,并将它们存储到字符串数组中。接下来,我们调用 `sort_strings` 函数对这个字符串数组进行排序,并使用 `for` 循环遍历已排序的字符串数组,输出每个字符串并在每个字符串之间添加一个空格。最后,我们输出一个换行符并返回 0。