代码自有黄金屋
http://www.kuqin.com/cpluspluslib/20071231/3265.html
//1这是一个一般用法的例子。
// priority_queue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1,"zl"));
mapStudent.insert(map<int, string>::value_type(2,"gs"));
//注意2,因为2这个下表map中是有的了,所以再插入2这个key是不会成功的。
//所以,输出结果:
//2 gs而不是hys
mapStudent.insert(map<int, string>::value_type(2,"hys"));
//注意3,怎么判断insert是否成功
pair<map<int,string>::iterator, bool> insert_pair;
insert_pair = mapStudent.insert(map<int, string>::value_type(2,"hys"));
cout << "结果成功或者失败:" << insert_pair.second << endl;
map<int, string>::iterator it;
map<int, string>::reverse_iterator rit;
for(it = mapStudent.begin(); it != mapStudent.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
cout << "---------------" << endl;
//注意1,实现反着遍历
for(rit = mapStudent.rbegin(); rit != mapStudent.rend(); rit++)
{
cout << rit->first << " "<< rit->second << endl;
}
return 0;
}
//2,稍微难点的应用
2.1在类中实现排序策略。有要注意的地方,细见代码。
注:好久没接触c++了,我一下没找到“注意1”的错误,LG奸笑
// priority_queue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
class student {
private:
unsigned int ID_;
string name_;
public:
student(unsigned int id, string name):ID_(id), name_(name)
{
}
unsigned int ID() const {
return ID_;
}
string name() const {
return name_;
}
//注意1,参数的const是必须的。否则会报:
//Error 1 error C2679: binary '<' : no operator found which takes a right-hand operand of type 'const student' (or there is no acceptable conversion) c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 priority_queue
//注意2,因为map中需要的,所以必须在这个地方重载这个符号
bool operator < (const student &s) const {
return (ID() < s.ID());
}
friend ostream& operator<< (ostream &o, const student &s) {
o << s.ID_ << " " << s.name_;
return o;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<student, int> stu;
student s1(1, "zl"), s2(2,"gs");
stu.insert(pair<student,int>(s1, 59));
stu.insert(pair<student,int>(s2, 80));
map<student, int>::iterator it;
for (it = stu.begin(); it != stu.end(); it++) {
cout << it->first << " " << it->second << endl;
}
return 0;
}
2.2.1好啦,我们来分析被LG鄙视的那个错误吧。就是上面代码的“注意1”的错误啊。
2.2.1.1在STL的实现上是这个模式:
bool operator < (const XX &t1, const XX &t2)
{
return t1 < t2;
}
2.2.1.2这里的<符号其实就是调用我在Student类中重载的<函数,调用规则我把它写出来:
t1.<(t2);
2.2.1.3嘻嘻道来哈~~
而,我在student类中的代码:
bool operator < (/*const*/ student &s) const {
return (ID() < s.ID());
}
2.2.1.4好啦,这里很重要了,总结了:
其实t2是const类型的,传给我的<重载函数,而<重载函数的参数类型是 非const2.2在一个类中实现策略
// priority_queue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
class student {
private:
unsigned int ID_;
string name_;
public:
student(unsigned int id, string name):ID_(id), name_(name)
{
}
unsigned int ID() const {
return ID_;
}
string name() const {
return name_;
}
//注意1,参数的const是必须的。否则会报:
//Error 1 error C2679: binary '<' : no operator found which takes a right-hand operand of type 'const student' (or there is no acceptable conversion) c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 priority_queue
//注意2,因为map中需要的,所以必须在这个地方重载这个符号
//bool operator < (const student &s) const {
// return (ID() < s.ID());
//}
friend ostream& operator<< (ostream &o, const student &s) {
o << s.ID_ << " " << s.name_;
return o;
}
};
//注意1,在类中实现策略
class cmp {
public:
bool operator()(const student& s1, const student &s2) const {
return s1.ID() < s2.ID();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<student, int, cmp> stu;
student s1(1, "zl"), s2(2,"gs");
stu.insert(pair<student,int>(s1, 59));
stu.insert(pair<student,int>(s2, 80));
map<student, int>::iterator it;
for (it = stu.begin(); it != stu.end(); it++) {
cout << it->first << " " << it->second << endl;
}
return 0;
}