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" 的头文件里 ...
随机推荐
- 02 java内存模型
java内存模型 1.JVM内存区域 方法区:类信息.常量.static.JIT (信息共享) java堆:实例对象 GC (信息共享) OOM VM stack:JAVA方法在运行的内存模型 (OO ...
- break和continue能否跳出函数
int func() { printf("In func, before continue.\n"); // continue; break; printf("In fu ...
- javascript的继承模式
在javascript里面看到javascript的继承模式和传统的继承模式是有区别的,就想查资料看一下到底有区别,就看到了这篇文章,觉得讲得还可以,暂时先放上来,以后有别的东西再补充: http:/ ...
- shell PATH示例
- 115-基于TI TMS320DM6467T Camera Link 机器视觉 智能图像分析平台
基于TI TMS320DM6467无操作系统Camera Link智能图像分析平台 1.板卡概述 该板卡是我公司推出的一款具有超高可靠性.效率最大化.无操作系统的智能视频处理卡,是机器视觉开发上的首选 ...
- python2和python3同时存在电脑时,安装包时的的命令行
若是在Python2中使用pip操作时,用pip2或是pip2.7相关命令. 例:给Python2安装selenium,在cmd中输入 pip2 install selenium 或是 pip2.7 ...
- Redis事件通知示例
1. redis如同zk一样,提供了事件监听(或者说是回调机制), 下面是redis的配置说明: ############################# EVENT NOTIFICATION ## ...
- 4,JPA
一,什么是JPA JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA(Java Pers ...
- PHP curl_exec函数
curl_exec — 执行一个cURL会话 说明 mixed curl_exec ( resource $ch ) 执行给定的cURL会话. 这个函数应该在初始化一个cURL会话并且全部的选项都被设 ...
- manacher 和 扩展KMP
manacher 和 扩展KMP 事实上,这两个东西是一样的. 考虑 manacher 的过程 我们实时维护最远扩展的位置 \(mx\) 以及这个回文串的回文中心 \(l\) ,那么显然当然位置如果没 ...