Cocos2d-x之Map<K, V>
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
Map<K, V>是Cocos2d-x 3.0x中推出的字典容器,它也能容纳Ref类型。Map<K,V>是模仿C++标准库的std::unorder_map<K, V>模板设计的。
创建Map<K,V>对象函数:
/** Default constructor Map()默认构造函数*/
Map<K, V>()
: _data()
{
static_assert(std::is_convertible<V, Ref*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the default constructor of Map!");
}
/** Constructor with capacity. 创建Map,并设置容量*/
explicit Map<K, V>(ssize_t capacity)
: _data()
{
static_assert(std::is_convertible<V, Ref*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the constructor with capacity of Map!");
_data.reserve(capacity);
}
/** Copy constructor. 用一个已经存在的Map创建另一个Map*/
Map<K, V>(const Map<K, V>& other)
{
static_assert(std::is_convertible<V, Ref*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the copy constructor of Map!");
_data = other._data;
addRefForAllObjects();
}
/** Move constructor. 用一个已经存在的Map创建另一个Map*/
Map<K, V>(Map<K, V>&& other)
{
static_assert(std::is_convertible<V, Ref*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the move constructor of Map!");
_data = std::move(other._data);
}
添加元素函数:
/** Inserts new elements in the map. @note If the container has already contained the key, this function will erase the old pair(key, object) and insert the new pair.param key The key to be inserted.param object The object to be inserted.在Map中添加一个新元素,V必须是Ref以及子类指针类型*/
void insert(const K& key, V object)
{
CCASSERT(object != nullptr, "Object is nullptr!");
object->retain();
erase(key);
_data.insert(std::make_pair(key, object));
}
移除元素的函数:
/** Removes an element with an iterator from the Map<K, V> container.param position Iterator pointing to a single element to be removed from the Map<K, V>.Member type const_iterator is a forward iterator type.指定位置移除对象,参数是迭代器,返回值是下一个迭代器*/
iterator erase(const_iterator position)
{
CCASSERT(position != _data.cend(), "Invalid iterator!");
position->second->release();
return _data.erase(position);
}
/** Removes an element with an iterator from the Map<K, V> container.param k Key of the element to be erased.Member type 'K' is the type of the keys for the elements in the container,defined in Map<K, V> as an alias of its first template parameter (Key).通过给定键移除一个指定位置的元素*/
size_t erase(const K& k)
{
auto iter = _data.find(k);
if (iter != _data.end())
{
iter->second->release();
_data.erase(iter);
return ;
}
return ;
}
/** Removes some elements with a vector which contains keys in the map.param keys Keys of elements to be erased.通过给定建集合移除多个元素*/
void erase(const std::vector<K>& keys)
{
for(const auto &key : keys) {
this->erase(key);
}
}
/** All the elements in the Map<K,V> container are dropped:their reference count will be decreased, and they are removed from the container,leaving it with a size of 0.移除所有的元素*/ void clear()
{
for (auto iter = _data.cbegin(); iter != _data.cend(); ++iter)
{
iter->second->release();
}
_data.clear();
}
查找元素的函数:
/** Returns a reference to the mapped value of the element with key k in the map.note If key does not match the key of any element in the container, the function return nullptr.param key Key value of the element whose mapped value is accessed.Member type K is the keys for the elements in the container. defined in Map<K, V> as an alias of its first template parameter (Key).*/
const V at(const K& key) const //通过键返回值
{
auto iter = _data.find(key);
if (iter != _data.end())
return iter->second;
return nullptr;
}
V at(const K& key) //返回指定整型键的值
{
auto iter = _data.find(key);
if (iter != _data.end())
return iter->second;
return nullptr;
}
/** Searches the container for an element with 'key' as key and returns an iterator to it if found,otherwise it returns an iterator to Map<K, V>::end (the element past the end of the container).param key Key to be searched for.Member type 'K' is the type of the keys for the elements in the container,defined in Map<K, V> as an alias of its first template parameter (Key).*/
const_iterator find(const K& key) const //查找Map容器中的对象,返回值迭代器
{
return _data.find(key);
}
iterator find(const K& key) //查找Map容器中的对象,返回值迭代器
{
return _data.find(key);
}
其他操作函数:
()、 std::vector<K> keys(); //返回所有的键 ()、 std::vector<K> keys(V object); //返回与对象匹配的所有的键值 ()、 ssize_t size(); //返回元素的个数
实例:
.h files #ifndef _MAPTEST_SCENE_H_
#define _MAPTEST_SCENE_H_
#include "cocos2d.h"
class mapTest : public cocos2d::Layer
{
private:
public:
static cocos2d::Scene* createScene();
virtual bool init();
void testMap();
CREATE_FUNC(mapTest);
};
#endif // _MAPTEST_SCENE_H_ .cpp files #include "MapTest.h"
USING_NS_CC;
Scene* mapTest::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = mapTest::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
} // on "init" you need to initialize your instance
bool mapTest::init()
{
{
return false;
}
testMap();
return true;
} void mapTest::testMap()
{
//1. 插入值
auto sp1 = Sprite::create();
sp1->setTag(0);
Map<std::string, Sprite*> map1;
std::string mapKey1 = "MAP_KEY_1";
map1.insert(mapKey1, sp1);
log("The size of map is %zd.", map1.size()); //2. 使用map1创建
Map<std::string, Sprite*>map2(map1);
std::string mapKey2 = "MAP_KEY_2"; //3. 判断是否为空
if (!map2.empty())
{
//根据key获得值
auto spTemp = (Sprite*)map2.at(mapKey1);
//获得sprite的tag
log("sprite tag = %d", spTemp->getTag());
//创建sprite
auto sp2 = Sprite::create();
//设置Tag
sp2->setTag(2);
//插入
map2.insert(mapKey2, sp2);
//key集合
std::vector<std::string>mapKeyVec;
//遍历key
for (auto key : mapKeyVec)
{
//根据key获得value
auto spTag = map2.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(), map2.bucket(key));
}
log("%zd buckets in the Map container", map2.bucketSize(2));
log("%zd element in bucket 1", map2.getRandomObject()->getTag()); //大小
log("Before remove sp1,size of map is %zd.", map1.size());
//查找并删除
map1.erase(map1.find(mapKey1));
log("After remove sp0,size of map is %zd.", map1.size());
} //5. 重新设置Map的大小
Map<std::string, Sprite*>map3(5);
map3.reserve(10);
}

Cocos2d-x之Map<K, V>的更多相关文章
- cocos基础教程(5)数据结构介绍之cocos2d::Map<K,V>
1.概述 cocos2d::Map<K,V> 是一个内部使用了 std::unordered_map的关联容器模版. std::unordered_map 是一个存储了由key-value ...
- 关于jsp利用EL和struts2标签来遍历ValueStack的东东 ------> List<Map<K,V>> 以及 Map<K,<List<xxx>>> 的结构遍历
//第一种结构Map<K,<List<xxx>>> <body> <% //显示map<String,List<Object>& ...
- JDK源码(1.7) -- java.util.Map<K,V>
java.util.Map<K,V> 源码分析 --------------------------------------------------------------------- ...
- Map<k,v>接口
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html public interface Map<K,V> K—key,V ...
- 随笔1 interface Map<K,V>
第一次写笔记就从map开始吧,如上图所示,绿色的是interface,黄色的是abstract class,蓝色的是class,可以看出所有和图相关的接口,抽象类和类的起源都是interface ma ...
- Mybatis返回List<Map<K,V>>
最终映射的字段名 会被作为 hashMap 的 key , <!-- TODO 测试返回 HashMap--> <resultMap id="testResultMap&q ...
- Map<K, V> 中k,v如果为null就转换
Set<String> set = map.keySet(); if(set != null && !set.isEmpty()) { for(String key : s ...
- Java------遍历Map<k,v>的方法
1. public class MapAction extends ActionSupport{ private Map<String, User> map = new HashMap&l ...
- Cocos2d-x3.0模版容器具体解释之二:cocos2d::Map<K,V>
1.概述: 版本号: v3.0 beta 语言: C++ 定义在 "COCOS2DX_ROOT/cocos/base" 路径下的 "CCMap.h" 的头文件里 ...
随机推荐
- Windows程序设计--(一)起步
1.3 你的第一个Windows程序 1.3.2 Windows 对应程序 #include <windows.h> int WINAPI WinMain(HINSTANCE hInsta ...
- vue 实现模糊检索,并根据其他字符的首字母顺序排列
昨天让我做一个功能,实现一个模糊检索,我就想,那做呗,然后开始正常的开发 代码如下: HTML VUE 因为是实时的,所以写了将逻辑写到了watch中 五分钟搞定. 我以为这就完了,然而产品的需求 ...
- php中__call与__callstatic()使用
php 5.3 后新增了 __call 与__callStatic 魔法方法. __call 当要调用的方法不存在或权限不足时,会自动调用__call 方法. __callStatic 当调用的静态方 ...
- php pdo_mysql扩展安装
本文内容是以 CentOS 为例,红帽系列的 Linux 方法应该都是如此,下面就详细说明步骤,在这里严重鄙视哪些内容??隆⑺档脑悠咴影说挠泄 PDO 编译安装的文章. 1.进入 PHP 的软件包 p ...
- ltp-ddt wdt_test
# @name Watchdog Timer getsupport,settimeout,getstatus,keepalive ioctl and write test# @desc Watchdo ...
- runltp出现问题 [
runltp 623行: if [ "$?" == "0" ]; then 对[解析出了问题. 我灵机一动,是不是sh的问题. which sh /bin/sh ...
- MinMax 容斥 学习笔记
基本形式 \[ \max(S) = \sum_{T\subseteq S, T \neq \varnothing} (-1)^{|T|-1}\min(T) \] 证明 不提供数学证明. 简要讲一下抽象 ...
- UOJ131 [NOI2015] 品酒大会
考前挣扎(bu shi 之前留下来的坑 首先注意到 SAM的parent树 是反串的后缀树 也就是原串的前缀树 这个性质很重要 所以说我们在树上统计的时候两个点的lca就是两个后缀串的lcp 于是可以 ...
- 媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
如今,双11不光是购物狂欢节,更是对技术的一次“大考”,对于阿里巴巴企业内部运营的基础保障技术而言,亦是如此. 回溯双11历史,这背后也经历过“小米加步枪”的阶段:作战室从随处是网线,交换机放地上的“ ...
- session应用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...