• 注意类STL——map


    最近研究注意类,略微总结一下,以后继续补充:

        代码自有黄金屋

        注意和类

        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型类的,传给我的<重载函数,而<重载函数的参数型类是

        

        非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;
    }

        


    文章结束给大家分享下程序员的一些笑话语录: 雅虎最擅长的不是开通新业务,是关闭旧业务。

  • 相关阅读:
    React开发小问题记录
    React 生命周期
    CSS 函数
    React props
    React State 状态
    React 组件 复合组件
    React JSX语法
    js动态创建标签,并设置样式。
    jq 二级筛选切换
    从算法看背包问题(1)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3061738.html
Copyright © 2020-2023  润新知