本文摘自c++标准程序库,仅供学习
撰写仿函数(方法),及实现运行期排序
/*
本程序设计一下几个知识点:
1,如何使用map
2,如何撰写和使用仿函数
3,如何在执行期定义排序准则
4,如何在“不在乎大小写”的情况下比较字符串
*/
#include<iostream>
#include<iomanip>
#include<map>
#include<algorithm>
using namespace std;
/*
function object to compare string
-allows you to set the comparsion criterion at runtime
-allows you to compare case insensitive
*/
class RuntimeStringCmp {
public:
enum cmp_mode { normal, nocase };
private:
const cmp_mode mode;
static bool nocase_compare(char c1, char c2) {
return toupper(c1) < toupper(c2);
}
public:
RuntimeStringCmp(cmp_mode m = normal) :mode(m) {};//默认为normal
bool operator()(const string& s1, const string& s2)const {
if (mode == normal) {
return s1 < s2;
}
else {
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(), nocase_compare);
}
}
};
/*
container type:
-map with
-string keys
-string values
-the special comparison object type
*/
typedef map<string, string, RuntimeStringCmp> StringStringMap;
void fillAndPrint(StringStringMap &coll);
int main() {
StringStringMap coll1;
fillAndPrint(coll1);
RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase);
StringStringMap coll2(ignorecase);//因为map有构造函数map c(op);op为其排序准则
fillAndPrint(coll2);
return 0;
}
void fillAndPrint(StringStringMap &coll) {
coll["Deutschland"] = "Germany";
coll["deutschl"] = "Germany";
coll["Haken"] = "snag";
coll["genhen"] = "walk";
StringStringMap::iterator pos;
cout.setf(ios::left, ios::adjustfield);
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << setw(15) << pos->first.c_str() << " " << pos->second.c_str() << endl;
}
cout << endl;
}
/*
lexicographical_compare(first1,last1,first2,last2,cmp)
它的作用是比较两个容器的字典序,如果返回1,说明第一个容器比第二个容器字典序小;如果返回2,说明第一个容器比第二个容器字典序大。
*/
map/mutlmap插入对象的三种方法
/*
安插一个key/value的时候,在map/multimap内部,把key当常数看。要么提供正确的型别,要不提供显示或隐式转换。
有三种方式可将value传入map
1,运用value_type
为避免隐式类型转换,可以利用value_type传输正确的类别
例:
std::map<std::string,float> coll;
coll.insert(std::map<std::string,float>::value_type("acvsa",21.050));
2,运用pair<>
直接运用pair<>。
例:
std::map<std::string,float> coll;
coll.insert(std::pair<std::string,float>("bash",2.01));
3,运用make_pair()
最常用make_pair()
例:
std::map<std::string,float> coll;
coll.insert(make_pair("bash",65.2));
*/
利用仿函数实现查找map中的指定value值
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
/*
function object to check the value of a map element
*/
template<class K,class V>
class value_equals {
private:
V value;
public:
//container initialize value to compare with
value_equals(const V& val):value(val){}
//comparison
bool operator()(pair<const K, V> elem) {
return elem.second == value;
}
};
typedef map<float, float> FloatFloatmap;
int main() {
FloatFloatmap coll;
FloatFloatmap::iterator pos;
coll[1] = 7;
coll[2] = 4;
coll[3] = 2;
coll[4] = 3;
coll[5] = 6;
coll[6] = 1;
coll[7] = 3;
pos = coll.find(3.0);//按key找
if (pos != coll.end()) {
cout << pos->first << " " << pos->second<<endl;
}
pos = find_if(coll.begin(), coll.end(), value_equals<float, float>(3.0));//按value找
if (pos != coll.end()) {
cout << pos->first << " " << pos->second << endl;
}
return 0;
/*
结果:
3 2
4 3
*/
}