22.cocos2d-x数据结构

前端之家收集整理的这篇文章主要介绍了22.cocos2d-x数据结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.Vector测试:

CCLOG("Vector Test");
            //一.Vector基本使用
            auto sp0 = Sprite::create();
            sp0->setTag(0);
            auto sp1 = Sprite::create();
            sp1->setTag(1);

            std::shared_ptr<Vector<Sprite*>>  vec0 = std::make_shared<Vector<Sprite*>>();  
            vec0->pushBack(sp0);            

            Vector<Sprite*>  vec1(5);
            //1.插入一条数据
            vec1.insert(0,sp1);
            //2.添加一条数据
            vec1.pushBack(*vec0);

            for (auto sp : vec1)
            {
                CCLOG("sprite tag = %d",sp->getTag());
            }

            Vector<Sprite*> vec2(*vec0);
            //3.Vector比较是否相等
            if (vec0->equals(vec2)) { //returns true if the two vectors are equal
                CCLOG("pVec0 is equal to pVec2");
            }

            //4.Vector判空
            if (!vec1.empty()) {
                //5.获取Vector的容量和大小/get the capacity and size of the Vector,noted that the capacity is not necessarily equal to the vector size.
                if (vec1.capacity() == vec1.size()) {
                    log("pVec1->capacity()==pVec1->size()");
                }else {
                    vec1.shrinkToFit();   //shrinks the vector so the memory footprint corresponds with the number of items
                    log("pVec1->capacity()==%zd; pVec1->size()==%zd",vec1.capacity(),vec1.size());
                }
                //6.清除Vector指定位置的元素
                //pVec1->swap(0,1); //swap two elements in Vector by their index 
                vec1.swap(vec1.front(),vec1.back());  //swap two elements in Vector by their value
                //7.Vector中是否包含某一元素
                if (vec2.contains(sp0)) {  //returns a Boolean value that indicates whether object is present in vector
                    log("The index of sp0 in pVec2 is %zd",vec2.getIndex(sp0));
                }
                //8.移除Vector中某一元素
                vec1.erase(vec1.find(sp0));
                //pVec1->erase(1);//移除指定位置的元素
                //pVec1->eraSEObject(sp0,true);//元素;是否removeAll
                //pVec1->popBack();//移除Vector中最后一个元素
                //9.清除Vector所有元素remove all elements
                vec1.clear(); 

                log("The size of pVec1 is %zd",vec1.size());
            }

/* 我们知道swap可通过调用复制构造函数与容器交换,来达到释放内存的目的,这是切实可行的,而且实际检测确实释放了内存; 然后新标准,也就是C++11也出了一个专用函数shrink_to_fit,用它来释放vector和deque容器中释放不掉的内存,但是这个函数有点搞笑,它并不保证一定会释放掉多余的内存,而且看编译器的脸色,也就是说它只是提出请求,如果请求被采纳,那么内存被释放,如果请求不被采纳,那么不好意思,不能释放。 */

2.Map测试:

CCLOG("Map Test");

            auto sp0 = Sprite::create();
            sp0->setTag(0);
            auto sp1 = Sprite::create();
            sp1->setTag(1);

            //1.插入一条数据
            Map<std::string,Sprite*> map0;
            map0.insert("MAP_KEY_0",sp0);
            log("The size of map is %zd.",map0.size());

            //2.创建一个capacity是5的Vector
            Map<std::string,Sprite*> map1(map0);
            //3.Map判空
            if (!map1.empty()) {
                //4.获取一个元素
                auto spTemp = (Sprite*)map1.at("MAP_KEY_0");
                log("sprite tag = %d",spTemp->getTag());               
                //map1.insert("MAP_KEY_1",sp1);
                //5.获取Vector所有的key
                std::vector<std::string> mapKeyVec;
                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());
                    //6.获取指定元素在Map中的位置
                    log("Element with key %s is located in bucket %zd",key.c_str(),map1.bucket(key));//格式符z和整数转换说明符一起使用,表示对应数字是一个size_t值。属于C99。一般很少使用,个人觉得意义不大。
                }

                log("%zd buckets in the Map container",map1.bucketCount());
                log("%zd element in bucket 1",map1.bucketSize(1));
                //get a random object if the map isn't empty,otherwise it returns nullptr
                log("The random object tag = %d",map1.getRandomObject()->getTag());
                //find(const K& key) can be used to search the container for an element with 'key'
                //erase(const_iterator position) remove an element with an iterator
                log("Before remove sp0,size of map is %zd.",map1.size());
                //7.删除一个元素
                map1.erase(map1.find("MAP_KEY_0"));
                log("After remove sp0,map1.size());
            }
            //8.创建并初始化Map的容量为5
            Map<std::string,Sprite*> map2(5);
            //9.设置Map的容量为10
            map2.reserve(10); 

    /* 设计者们谨慎地设计了cocos2d::Map<K,V>用来替代cocos2d::CCDictionary,所以应该尽量使用cocos2d::Map而不是cocos2d::CCDictionary 当在栈中声明cocos2d::Map<K,V>对象时,无需费心释放它占用的内存。 但是如果你是使用new操作来动态分配cocos2d::Map<K,V>的内存的话,就得用delete来释放内存了,new[]操作也一样。 如果真心想动态分配堆cocos2d::Map<K,V>,请将原始指针用智能指针来覆盖。 警告:cocos2d::Map<K,V>并不是cocos2d::Object的子类,所以不要像使用其他cocos2d类一样来用retain/release和引用计数内存管理。 */

3.Value测试:

CCLOG("Value Test");
            Value val;   // call the default constructor
            //1.判空
            if (val.isNull()) {
                log("val is null");
            }
            else {
                std::string str = val.getDescription();
                log("The description of val0:%s",str.c_str());
            }
            //2.基本数据---------------------------------------------------- 
            Value val1(65);   // initialize with a integer
                              //Value val1(3.4f); // initialize with a float value
                              //Value val1(3.5); // initialize with a double value
            log("The description of the integer value:%s",val1.getDescription().c_str());
            log("val1.asByte() = %c",val1.asByte());//A对应的ASCII值
            //3.字符串----------------------------------------------------
            std::string strV = "string";
            Value val2(strV);   // initialize with string
            log("The description of the string value:%s",val2.getDescription().c_str());
            //4.Vector----------------------------------------------------
            auto sp0 = Sprite::create();
            Vector<Object*>* vecV = new Vector<Object*>();
            vecV->pushBack(sp0);
            Value val3(vecV);   // initialize with Vector
            log("The description of the Vector value:%s",val3.getDescription().c_str());
            delete vecV;
            //5.Map----------------------------------------------------
            Map<std::string,Object*>* mapV = new Map<std::string,Object*>();
            mapV->insert(strV,sp0);
            Value val4(mapV);   // initialize with Map
            log("The description of the Map value:%s",val4.getDescription().c_str());
            delete mapV;
            //5.Map----------------------------------------------------
            Value val6(&val4);   // initialize with Map
            log("The description of the Value-type value:%s",val6.getDescription().c_str());
            //2.字符串转整数----------------------------------------------------
            val2 = val1;   // assigning between 2 Value-type
            log("operator-> The description of val2:%s",val2.getDescription().c_str());
            val2 = 4;   //assigning directly
            log("operator-> The description of val4:%s",val2.getDescription().c_str());


/* 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的内存由它的析构函数来释放,所以使用cocos2d::Value时请尽量用推荐的最佳做法。注意:cocos2d::Value不能像其它cocos2d类型一样使用retain/release和refcount内存管理 */
原文链接:https://www.f2er.com/cocos2dx/340439.html

猜你在找的Cocos2d-x相关文章