Playground
//koristi C++11 - http://mendo.mk/Lecture.do?id=26 #include <bits/stdc++.h> using namespace std; //se koristi od stable_sort() podolu bool stable_sort_by_length(string a, string b) { return a.size() < b.size(); } //se koristi od sort() podolu bool sort_by_length_and_pos(pair<string, int> a, pair<string, int> b) { string name_a = a.first; int pos_a = a.second; string name_b = b.first; int pos_b = b.second; if(name_a.size() == name_b.size()) { //podredi po pozicija ako imaat ednakva dolzhina return pos_a < pos_b; } //razlichna dolzhina, pa podredi po toa return name_a.size() < name_b.size(); } int main() { vector<string> names = {"Marko", "Petar", "Ana", "Maja", "Igor", "Darko"}; //opcija 1, koristejki stable_sort() stable_sort(names.begin(), names.end(), stable_sort_by_length); //opcija 2, koristejki sort() vector<pair<string, int> > names_with_position; for(int i=0; i < names.size(); i++) { string name = names[i]; names_with_position.push_back( {name, i} ); } sort(names_with_position.begin(), names_with_position.end(), sort_by_length_and_pos); return 0; }
Input data
Program output
Execute
Language: ????????? | Memory: ???? KB | Time: ??? ms
Nothing has been executed, yet!