-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ]

, 08 2017 . 14:35 +
, , , , , , . , , , .

, . , , , , , . , , , .

, ( - ), . , . .

const int n = 15;
string cities[n] = {
	"", "", "", "", "", 
	"", "", "", "", "", 
	"", "", "", "", ""
};

(/) (/). , , , . , - . , , .

, ? , -, ? . , , . , .

, . :

typedef bool(*CompareFunction) (int i, int j);

. , CompareFunction, -, . . , . :

bool compareUp(int i, int j) {
	return cities[i] < cities[j];
}

bool compareDown(int i, int j) {
	return cities[i] > cities[j];
}

bool compareLengthUp(int i, int j) {
	return cities[i].length() < cities[j].length();
}

bool compareLengthDown(int i, int j) {
	return cities[i].length() > cities[j].length();
} 

:

CompareFunction compares[] = {compareUp, compareDown, compareLengthUp, compareLengthDown};

:

void sortCities(CompareFunction compare) {
	for (int i = 0; i < n - 1; i++) {
		int j_min = i;
		for (int j = i+1; j < n; j++) {
			if (compare(j, j_min)) {
				j_min = j;
			}
		}
		string temp = cities[i];
		cities[i] = cities[j_min];
		cities[j_min] = temp;
	}
}

? : compare , , .

, , - user interface, .

int _tmain(int argc, _TCHAR* argv[]) {
	setlocale(LC_ALL, "Russian");
	
	for (;;) {
		int choice;
		cout << " : ";
		cin >> choice;
		sortCities(compares[choice]);
		printCities();
	}
	system("pause");
	return 0;
}

. , , . .

P.S. printCities() , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/335162/

:  

: [1] []
 

:
: 

: ( )

:

  URL