元素排序2

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

bool lessLength(const string& s1, const string& s2)
{
	return s1.length() < s2.length();
}

int main()
{
	vector<string> svec;
	vector<string> svec2;

	svec.push_back("1cui");
	svec.push_back("2c");
	svec.push_back("3cu");
	svec.push_back("4cui");

	svec2 = svec;

	for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;

	sort(svec.begin(), svec.end(), lessLength); // sort是没有保证的,相对于stable_sort
	for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;

	stable_sort(svec.begin(), svec.end(), lessLength);  // stable_sort是稳定的排序,可以保证原来相同的大小还是按照原来的顺序,
	for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;


	return 0;
}

你可能感兴趣的:(元素排序2)