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

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

cocos2dx 3.x Value、Vector和Map意识的更多相关文章

  1. Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法

    Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...

  2. paip.提升性能---list,arraylist,vector,linkedlist,map的选用..

    paip.提升性能---list,arraylist,vector,linkedlist,map的选用.. arraylist,vector基本一样,但是,vector线程安全的. 作者Attilax ...

  3. pair在vector和map中的使用

    #include <iostream> #include <cstdio> #include <cstring> #include <stack> #i ...

  4. Java容器类List、ArrayList、Vector及map、HashTable、HashMap的区别与用法

    Java容器类List.ArrayList.Vector及map.HashTable.HashMap的区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数 ...

  5. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  6. C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法

    每次忘记都去查,真难啊 /* C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法 */ /* vector常用用法 */ //头文件 #i ...

  7. 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27705613 容器 3.0版本号之前Cocos2d- ...

  8. cocos2d-x之Vector与map

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  9. c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑

    转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...

随机推荐

  1. UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)

    一个n个点的凸多边形,求多边形中离多边形边界最远的距离.实际上就是求凸包最大内接圆的半径. 利用半平面交求解,每次二分枚举半径d,然后将凸包每条边所代表的半平面沿其垂直单位法向量平移d,看所有平移后的 ...

  2. The JSR-133 Cookbook for Compiler Writers(an unofficial guide to implementing the new JMM)

    The JSR-133 Cookbook for Compiler Writers by Doug Lea, with help from members of the JMM mailing lis ...

  3. 【SICP归纳】2 高阶函数和数据抽象

    上一篇博文相应的是书中的第一章的一二两节,我们已经大致的有了一种构造的感觉不是么. 书中展示了非常多有趣的句法(syntax). 如今我们要让思想进一步的抽象.写这篇博客的时候并未学完整本书.更不敢说 ...

  4. Codeforces 9A-Die Roll(意甲冠军)

    A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...

  5. sails中文文档地址

    http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby on Rails的MVC架构的 ...

  6. 直接选择排序----java实现

    直接选择排序思路: 从待排序数据中选择第一个假定为最小的下标,然后他后面的与他循环比较,得到真的最小值下标,然后最小值前的那一区段依次后移,并把最小值赋值给第一个元素.第二次时,假定第二个为最小,然后 ...

  7. 华为-on演习--身高找到最好的二人

    称号: 从5个人选择2作为个人礼仪器.中的每个个体的身高的范围160-190,要求2各高度差值至少(假设差异值同样的事情,他们中最高的选择),输出的两个人的身高升序. Smple input:161  ...

  8. UI设计学习路径(一个)—好酒也怕巷子深

    来源 參与米老师对项目的验收的时候.听了老师对UI的看法才注意UI这块内容.非常奇怪为什么我们总是不能注意到本该注意的问题呢?软件开发难道仅仅是功能的实现不包含界面设计吗?当然不是.问题的根源在于我们 ...

  9. Linux 在下面MATLAB下载

    网上找了很长一段时间都没有很好的连接,或者是一个普通的软件, 在这里,给大家一个连接: ed2k://|file|[%E7%9F%A9%E9%98%B5%E5%AE%9E%E9%AA%8C%E5%AE ...

  10. Python 获取Twitter用户与Friends和Followers的关系(eg, 交集,差集)

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-30 @author: guaguastd @name: f ...