zoukankan      html  css  js  c++  java
  • cocos2dx 3.x Value、Vector和Map意识

    1. Value

    cocos2d::Value 这包括一个非常大的数字原生类型(int,float,double,bool,unsigned char,char* 和 std::string)外

    std::vector<Value>std::unordered_map<std::string,Value> 和 std::unordered_map<int,Value> 的类。

    你能够把全部上面的提及的原生类型放入 cocos2d::Value 对象中,然后将它们转化为相应的原生类型。反之亦然。

    Value val;   // 调用默认构造函数
    if (val.isNull()) {
        log("val is null");
    }else{
        std::string str =val.getDescription();
        log("The description of val0:%s",str.c_str());
    }
    //----------------------------------------------------
    Value val1(65);   // 用一个 int 初始化
    //Value val1(3.4f);   // 用一个 float 初始化
    //Value val1(3.5);   // 用一个 double 初始化
    log("The description of the integer value:%s",val1.getDescription().c_str());
    log("val1.asByte() = %c",val1.asByte());
    //----------------------------------------------------
    std::string strV = "string";
    Value val2(strV);   // 用 string 初始化
    log("The description of the string value:%s",val2.getDescription().c_str());
    //----------------------------------------------------
    auto sp0 = Sprite::create();
    Vector<Object*>* vecV = new Vector<Object*>();
    vecV->pushBack(sp0);
    Value val3(vecV);   // 用 Vector 初始化
    log("The description of the Vector value:%s",val3.getDescription().c_str());
    delete vecV;
    //----------------------------------------------------
    Map<std::string, Object*>* mapV = new Map<std::string, Object*>();
    mapV->insert(strV,sp0);
    Value val4(mapV);   // 用 Map 初始化
    log("The description of the Map value:%s",val4.getDescription().c_str());
    delete mapV;
    //----------------------------------------------------
    Value val6(&val4);   // 用 Map 初始化
    log("The description of the Value-type value:%s",val6.getDescription().c_str());
    //----------------------------------------------------
    val2 = val1;   // 在两个不同指类型间赋值
    log("operator-> The description of val2:%s",val2.getDescription().c_str());
    val2 = 4;   // 直接赋值
    log("operator-> The description of val4:%s",val2.getDescription().c_str());
    输出:
    
    cocos2d: val is null
    cocos2d: The description of the integer value:
    65
    
    cocos2d: val1.asByte() = A
    cocos2d: The description of the string value:
    string
    
    cocos2d: The description of the Vector value:
    true
    
    cocos2d: The description of the Map value:
    true
    
    cocos2d: The description of the Value-type value:
    true
    
    cocos2d: operator-> The description of val2:
    65
    
    cocos2d: operator-> The description of val4:
    4

    Value的作用和使用方法:在创建Value时,往构造函数里传入一个值。Value就会自己主动依据这个值来决定自己的类型。在获取Value的值时,就依据它的类型。调用as**函数获取。


    整数、浮点型和字符串之间的转换

    整型转为字符串: std::string str = "NO"+Value(1).asString();

    字符串转为整型:log("%d",Value("1234").asInt())

    浮点型转字符串:log("%s",Value(123.5f).asString().c_str())

    字符串转浮点型:log("%f",Value("14.45").asFloat())

    2. Vector

    Vector是一个封装好的能动态增长顺序訪问的容器。

    主要使用的函数说明:

    size():Vector大小

    at(index):返回Vector下标为index的对象

    pushBack(object):在Vector的最后加入一个object对象

    eraseObject(object):从Vector中移除object对象

    erase(index):从Vector中移除下标为index的对象

    clear():清空Vector

    怎样遍历Vector

    for(auto obj : vector){

    ...

    }

    3.Map

    Map是一个存储键值对的关联式容器,它能够通过它们的键高速检索相应的值。

    主要函数:

    insert(key,value):向Map中插入一个对象。

    at(key):返回Map中keyword为key的对象

    怎样遍历Map?

    mapKeyVec = map1.keys();
        for(auto key : mapKeyVec)
        {
            auto spTag = map1.at(key)->getTag();
            log("The Sprite tag = %d, MAP key = %s",spTag,key.c_str());
            log("Element with key %s is located in bucket %zd",key.c_str(),map1.bucket(key));
        }


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    u盘重装ubuntu16.04过程遇到的问题
    CTC安装及其错误解决办法:binding.cpp:92:49: error: cannot convert ‘THCudaTensor*’ to ‘const THFloatTensor*’ for argument ‘1’ to ‘int64_t THFloatTensor_size(const THFloatTensor*, int)’
    CTC安装错误之:binding.cpp:6:29: fatal error: torch/extension.h: No such file or directory
    机器学习入门(1)
    python学习笔记(1)
    python学习笔记(1)
    rpm命令的简介-(转自jb51.net )
    centos下安装visual studio code-(来自官网)
    ubuntu中安装visual studio code-(转载)
    git使用简单教程-(转自linux人)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4673169.html
Copyright © 2011-2022 走看看