zoukankan      html  css  js  c++  java
  • #define 的一些用法 以及 迭代器的 [] 与 find()函数的区别

    #include "stdafx.h"
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    #define fn(x) int a##x;        //声明一个ax变量  a常量  x变量
    #define tn(x) #x            //可以直接拼一个字符串  int  可以直接输出 int
    
    //可以帮你写一个类
    #define cclass(cname,x,y,z) class C##cname
    {
    public:
        x m_##x;
        y m_2##y;
        char m_##z;
    };
    
    /////////////////////////////////////////////////////////////////////////
    template <class ContainersType>
    void PrintContainersData(ContainersType containers)
    {
        ContainersType::iterator begin = containers.begin(), 
                                 end = containers.end();
        for(;begin!=end;begin++)
        {
            cout<<"Key = "<<begin->first<<"	"<<"Value = "<<begin->second<<endl;
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        cout<<"First output :"<<tn(int)<<endl;
        cclass(Hero,int,int,heroName);            //注意:此处用到了 定义的宏
        CHero h1;
    
        fn(3);
        a3 = 9;
        cout<<"Second output a3 = "<<a3<<endl;
        /////////////////////////////////////////////////////////////////////////
        map<string,float> m;
        m["Tom"] = 85;
        m["Jack"] = 80;
        m["Jack"] = 90;                            //注意:下面的会将上面的成绩修改
    
        cout<<"Third output : "<<endl;
        map<string,float>::iterator it = m.begin();
        for(; it!=m.end();it++)
        {
            cout<<it->first<<"   "<<it->second<<endl;
        }
    
        cout<<"Forth output : "<<endl;
        PrintContainersData(m);
    
        //m.insert(make_pair("Jack",100));        //注意:如果有相同的  则不可插入
        m.insert(map<string,float>::value_type("Jack",100));
    
    
        //int t = m["ww"];                        //注意[]:找不到 会自动添加这个名字
        cout<<"Fifth output : "<<endl;
        map<string,float>::iterator it2 = m.find("ww");   //注意find():找不到  会处理
        if(it2==m.end())
            cout<<"not find"<<endl;
    
    
        return 0;
    }

    运行结果 如下图:

  • 相关阅读:
    MVC中CheckBox
    Python中的高级数据结构
    高级正则表达式技术(Python版)
    程序员可以兼任项目经理吗?
    浅谈五大Python Web框架
    学习Python编程的11个资源
    Python 代码性能优化技巧
    python多线程ctrl-c退出问题
    Python 笔记 : 类和继承
    Python的OO思想
  • 原文地址:https://www.cnblogs.com/MrGreen/p/3479554.html
Copyright © 2011-2022 走看看