今天写一道csp题的时候,因为map的key的自动排序,差点搞死我
1、取消map的sort排序(只能取消number类型的)(而且的逆序输出)
#include <iostream>
#include <map>
using namespace std;
template<class T>
struct DisableCompare : public std::binary_function<T, T, bool>{
bool operator()(T lhs, T rhs) const{
return true;
}
};
int main(){
std::map<int, double, DisableCompare<int> > map1;
map1.insert(pair<int,double>(1, 1.3));
map1.insert(pair<int,double>(5, 5.3));
map1.insert(pair<int,double>(2, 4.3));
map1.insert(pair<int,double>(6, 2.3));
map1.insert(pair<int,double>(4, 0.3));
map<int,double>::reverse_iterator it = map1.rbegin();
for (; it != map1.rend(); it++)
{
cout << it->first << "=>" << it->second << endl;
}
return 0;
}
结果
2、替换map的sort排序(替换string类型为例)
typedef pair<string,string> spair__;
typedef vector<spair__> vector_type;
vector_type vec;
int main(){
int n;cin >> n;
while(n){
string key,value;
cin >> key >> value;
vet.push_back(spair__(key,value));
n--;
}
for(vector_type::iterator iter = vec.begin(); iter != vec.end(); iter++){
cout << iter->first << "->" << iter->second << endl;
}
return 0;
}