[ C++ ] sorting with index
#include<iostream>
#include<algorithm>
using namespace std;
struct Comparator
{
Comparator(const int* data) : m_data(data) {}
bool operator() (int left, int right) const { return m_data[left] < m_data[right]; }
const int* m_data;
};
int main()
{
int data[] = {16,2,77,29};
int index[] = {0,1,2,3};
std::sort(index, index + sizeof(index) / sizeof(int), Comparator(data));
for (int i = 0; i < sizeof(index) / sizeof(int); ++i) {
cout << index[i] << "\t";
}
return 0;
}
- http://blog.csdn.net/niuqingpeng/article/details/12649815
#include<algorithm>
using namespace std;
struct Comparator
{
Comparator(const int* data) : m_data(data) {}
bool operator() (int left, int right) const { return m_data[left] < m_data[right]; }
const int* m_data;
};
int main()
{
int data[] = {16,2,77,29};
int index[] = {0,1,2,3};
std::sort(index, index + sizeof(index) / sizeof(int), Comparator(data));
for (int i = 0; i < sizeof(index) / sizeof(int); ++i) {
cout << index[i] << "\t";
}
return 0;
}
- http://blog.csdn.net/niuqingpeng/article/details/12649815
留言
張貼留言