方法一:
代码语言:javascript复制#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool cmp1(pair<double,double> a, pair<double,double> b)
{
if (a.second < b.second)
return true;
else
return false;
}
template <typename T>
vector<int> sort_Index(vector<T>& data)
{
vector<int> index(data.size());
//赋值0-data.size()-1
iota(index.begin(),index.end(),0);
sort(index.begin(),index.end(),
[&data](int i,int j){return data[i]<data[j];});
// sort(index.begin(),index.end(),
// [&data](int i,int j){return data[i].second<data[j].second;});
sort(data.begin(),data.end());
return index;
}
void print(const int& tmp)
{
cout<<tmp<<endl;
}
int main()
{
//测试1
vector<int> data={3,1,4,2,5};
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i]<<endl;
}
cout<<"*****"<<endl;
//高级查找
for_each(data.begin(),data.end(),print);
vector<int>::iterator location_index;
location_index=find(data.begin(),data.end(),2);//数字2的下标
cout<<(location_index-data.begin())<<endl;
location_index=find_if(data.begin(),data.end(),bind2nd(greater<int>(),4));//第一个大于4的下标
cout<<(location_index-data.begin())<<endl;
cout<<"*****"<<endl;
vector<int> index;
index=sort_Index(data);//内部顺序不变
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i]<<endl;
}
cout<<"*****"<<endl;
for(unsigned int i=0; i<index.size();i )
{
cout<<index[i]<<endl;
}
//测试2
/* vector<pair<int,int>> data;
data.push_back(make_pair(3,2));
data.push_back(make_pair(1,1));
data.push_back(make_pair(2,3));
data.push_back(make_pair(4,5));
data.push_back(make_pair(5,4));
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i].second<<endl;
}
cout<<"*****"<<endl;
vector<int> index;
index=sort_Index(data);//内部顺序不变
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i].second<<endl;
}
cout<<"*****"<<endl;
for(unsigned int i=0; i<index.size();i )
{
cout<<index[i]<<endl;
}*/
return 0;
}
方法二:
代码语言:javascript复制#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool cmp1(pair<double,double> a, pair<double,double> b)
{
if (a.second < b.second)
return true;
else
return false;
}
struct Node
{
// int value;//类型可以根据需要该
pair<int,int> value;
int index;
};
bool cmp(struct Node& a, struct Node& b)
{
// if (a.value<b.value)//此处根据value的类型来定书写规则
if(a.value.second < b.value.second)
return true;
else
return false;
}
template <typename T>
T sort_Index(vector<int>& index, vector<T>& data)
{
Node* _data=new Node[data.size()];
for(unsigned int i=0;i<data.size();i )
{
_data[i].value=data[i];
_data[i].index=i;
}
sort(data.begin(),data.end(),cmp1);
sort(_data,_data data.size(),cmp);
for(unsigned int i=0;i<data.size();i )
{
index.push_back(_data[i].index);
}
delete[] _data;
}
int main()
{
//测试1
/* vector<int> data={3,1,4,2,5};
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i]<<endl;
}
cout<<"*****"<<endl;
vector<int> index;
sort_Index(index,data);//内部顺序不变
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i]<<endl;
}
cout<<"*****"<<endl;
for(unsigned int i=0; i<index.size();i )
{
cout<<index[i]<<endl;
}*/
//测试2
vector<pair<int,int>> data;
data.push_back(make_pair(3,2));
data.push_back(make_pair(1,1));
data.push_back(make_pair(2,3));
data.push_back(make_pair(4,5));
data.push_back(make_pair(5,4));
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i].second<<endl;
}
cout<<"*****"<<endl;
vector<int> index;
sort_Index(index,data);//内部顺序不变
for(unsigned int i=0; i<data.size();i )
{
cout<<data[i].second<<endl;
}
cout<<"*****"<<endl;
for(unsigned int i=0; i<index.size();i )
{
cout<<index[i]<<endl;
}
return 0;
}