(map&C++)(六、取消和替换sort排序)

今天写一道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类型为例)

//模拟map使用
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;

}

发布了42 篇原创文章 · 获赞 40 · 访问量 955

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/104575630