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" 的头文件里 ...
随机推荐
- python学习第十一天列表的分片和运算
列表的分片也叫切片,也就是从列表中取出一段赋值给另外一个变量,列表运算就是可以进行比较运算,连接运算,乘法运算等. 1,列表的分片 n1=[1,2,3,4,5,6,7,8,9] n2=[1:3] 包含 ...
- Spring bean相关
Spring中指定Bean的作用于的方式 以下四种为例: 单例(默认,可以不用特殊表明) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) ...
- DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法
原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA ...
- MVC模型的基本原理及实现原理
[转载]MVC架构在Asp.net中的应用和实现 摘要:本文主要论述了MVC架构的原理.优缺点以及MVC所能为Web应用带来的好处.并以“成都市信息化资产管理系统”框架设计为例,详细介绍其在Asp.n ...
- java类的加载与初始化
https://blog.csdn.net/u013349237/article/details/71076617 1在命令行启动虚拟机jvm进行加载, 2用class.forname()方法进行动态 ...
- shell编程基础知识3
1.Linux下scp的用法 scp就是secure copy,一个在linux下用来进行远程拷贝文件的命令.有时我们需要获得远程服务器上的某个文件,该服务器既没有配置ftp服务器,也没有做共享,无法 ...
- springmvc中拦截器配置格式
对于springmvc,有两种方式配置拦截器. 一是实现HandlerInterceptor接口,如 public class MyInterceptor1 implements HandlerInt ...
- leetcode-166周赛-5282-转化为全0矩阵的最小反转次数
题目描述: 方法一:暴力BFS class Solution: def minFlips(self, mat) -> int: R, C = len(mat), len(mat[0]) def ...
- vue 生命周期函数详解
beforeCreate( 创建前 ) 在实例初始化之后,数据观测和事件配置之前被调用,此时组件的选项对象还未创建,el 和 data 并未初始化,因此无法访问methods, data, compu ...
- 改计算机名导致 Oracle因目标主机或对象不存在
手贱修改了计算机名, 结果导致登陆oracle数据库报如下错误,一查资料,说是修改了计算机名导致的,需要进到oracle安装目录: \oracle\product\10.2.0\db_1\NETWOR ...