注意類STL——map

6130 ワード

最近注意類を研究して、少し総括して、後で引き続き補充します:
コード自有黄金屋
     注意和类
    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;

}


毎日同じ理屈
共和国は彼女の50歳の誕生日を迎えた.50年は長い川のようで、急流もあれば緩流もある.50年は長い巻きのようで、冷たい色も暖かい色もあります.50年は楽曲のようで、低音も高音もあります.50年は史詩のようで、苦痛もあれば喜びもある.長河は永遠に奔流し、絵巻は展開されたばかりで、楽曲はだんだんクライマックスになり、史詩はまだ書き続けている.われわれの共和国は確固たる歩みを歩み、新しい時代に突入している.
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はい、ここは重要です.まとめました.
実はt 2はconst型クラスで、私に伝わった<リロード関数、<リロード関数のパラメータ型クラスは
    
非const
    
    
2.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;

}

    
ヤフーが一番得意なのは、新しい業務を開通することではなく、古い業務を閉鎖することです.