标签:== names std str 字符 运算 space temp ios
两种方式
一:直接排序就行了。对于字符串“>、<、==、+”这些运算符都被重载了,可以直接用。
#include <iostream>
using namespace std;
#include <string>
 
int main()
{
	int num;
	cin >> num;
	string s[1000], temp;
	for (int i = 0; i < num; i++) {
		cin >> s[i];
	}
	for (int i = 0; i < num-1; i++) {
		for (int j = 0; j < num-i-1; j++) {
			if (s[j] > s[j+1]) {
				temp = s[j];
				s[j] = s[j+1];
				s[j+1] = temp;
			}
		}
	}
	for (int i = 0; i < num; i++)
		cout << s[i] << endl;
 
	return 0;
}
二;采用STL封装的结构和函数
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int main()
{
	int input;
	while (cin >> input)
	{
		string str;
		vector<string> vs;
		while (input--)
		{
			cin >> str;
			vs.push_back(str);
		}
		sort(vs.begin(), vs.end());
		vector<string>::iterator vit;
		for (vit = vs.begin(); vit != vs.end(); vit++)
		{
			cout << *vit << endl;
		}
	}
 
	return 0;
}
标签:== names std str 字符 运算 space temp ios
原文地址:https://www.cnblogs.com/xufeng123/p/12628896.html