user-0 提问: 请大佬指点

回复:
user: user-0
content: 引用 8 楼 sankernel 的回复:#include <algorithm> #include <iostream> #include <map> #include <vector>  struct Employee {     std::string id;     std::string name;     std::string birthDay;     int salary; };  // 按值升序排序 int CompUp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)   {   	return x.second < y.second;   }  // 按值降序排序 int CompDown(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)   {   	return x.second > y.second;   }  void SortMapByValueUp(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector) {   	for(std::map<std::string,int>::iterator it = tMap.begin(); it != tMap.end(); it++)   	{   		tVector.push_back(make_pair(it->first,it->second)); 	}  	std::sort(tVector.begin(), tVector.end(), CompUp); }  void SortMapByValueDown(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector) {   	for(std::map<std::string,int>::iterator it = tMap.begin(); it != tMap.end(); it++)   	{   		tVector.push_back(make_pair(it->first,it->second)); 	}  	std::sort(tVector.begin(), tVector.end(), CompDown); }  void Test() {     std::map<std::string, Employee&> mapID2Employee;     std::map<std::string, int> mapID2Salary;      mapID2Salary.insert(std::pair<std::string, int>("1001", 20000));     mapID2Salary.insert(std::pair<std::string, int>("1002", 25000));     mapID2Salary.insert(std::pair<std::string, int>("1003", 15000));      std::vector<std::pair<std::string, int>> vecAfterSort;     SortMapByValueUp(mapID2Salary, vecAfterSort);     for (std::vector<std::pair<std::string, int>>::iterator it = vecAfterSort.begin(); it != vecAfterSort.end(); ++it)     {         std::string sID = it->first;         int salary = it->second;          std::cout << sID.c_str() << " " << salary << std::endl;          // 再根据mapID2Employee[ID]获取Employee信息     } }  int main() {     Test();      system("pause");      return 0; }   谢谢，这是c++版本吧

user: user-1
content: #include <algorithm> #include <iostream> #include <map> #include <vector>  struct Employee {     std::string id;     std::string name;     std::string birthDay;     int salary; };  // 按值升序排序 int CompUp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)   {   	return x.second < y.second;   }  // 按值降序排序 int CompDown(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)   {   	return x.second > y.second;   }  void SortMapByValueUp(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector) {   	for(std::map<std::string,int>::iterator it = tMap.begin(); it != tMap.end(); it++)   	{   		tVector.push_back(make_pair(it->first,it->second)); 	}  	std::sort(tVector.begin(), tVector.end(), CompUp); }  void SortMapByValueDown(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector) {   	for(std::map<std::string,int>::iterator it = tMap.begin(); it != tMap.end(); it++)   	{   		tVector.push_back(make_pair(it->first,it->second)); 	}  	std::sort(tVector.begin(), tVector.end(), CompDown); }  void Test() {     std::map<std::string, Employee&> mapID2Employee;     std::map<std::string, int> mapID2Salary;      mapID2Salary.insert(std::pair<std::string, int>("1001", 20000));     mapID2Salary.insert(std::pair<std::string, int>("1002", 25000));     mapID2Salary.insert(std::pair<std::string, int>("1003", 15000));      std::vector<std::pair<std::string, int>> vecAfterSort;     SortMapByValueUp(mapID2Salary, vecAfterSort);     for (std::vector<std::pair<std::string, int>>::iterator it = vecAfterSort.begin(); it != vecAfterSort.end(); ++it)     {         std::string sID = it->first;         int salary = it->second;          std::cout << sID.c_str() << " " << salary << std::endl;          // 再根据mapID2Employee[ID]获取Employee信息     } }  int main() {     Test();      system("pause");      return 0; }

user: user-0
content: 引用 4 楼 與蘤舊縐 的回复:插排容易点吧，简单例子给你一个，数组插入排序     void exchange_item(int *num, int i, int j) {     int temp = *(num + i);     for (int pos = i; pos > j; pos--) {         *(num + pos) = *(num + pos - 1);     }     *(num + j) = temp; }  //free to choose the sort range void insertion_sort(int *num, int len) {     for (int i = 1; i < len; i++) {         if (*(num + i) < *(num + i - 1)) {             for (int j = 0; j < i; j++) {                 if (*(num + j) >= *(num + i)) {                     exchange_item(num, i, j);                     break;                 }             }         }     } }  好的，谢谢，我消化消化

user: user-2
content: 插排容易点吧，简单例子给你一个，数组插入排序    void exchange_item(int *num, int i, int j) {     int temp = *(num + i);     for (int pos = i; pos > j; pos--) {         *(num + pos) = *(num + pos - 1);     }     *(num + j) = temp; }  //free to choose the sort range void insertion_sort(int *num, int len) {     for (int i = 1; i < len; i++) {         if (*(num + i) < *(num + i - 1)) {             for (int j = 0; j < i; j++) {                 if (*(num + j) >= *(num + i)) {                     exchange_item(num, i, j);                     break;                 }             }         }     } }

user: user-0
content: 好的，谢谢，我试一试

user: user-3
content: 好像说错了，输入过程排序好像效率高一些

user: user-3
content: 怎么排序都可以，关键是考虑效率，每次输入都排序估计效率会低，个人觉得输入完后再排序好一点 排序方法和数组一样，只不过数组用下标(比如冒泡 if a[i] > a[i+1]) )，链表用next指针而已( if (a.data > a.next.data) )，思路都是一样的