COCOS2D-X用到的部分容器简单用法
1.vector#includestd::vector str;// 插入到末尾str.push_back(111);str.push_back(222);str.push_back(333);CCLOG("-------------------------");// 注意i是一个返回的迭代器,str.end()[0]指向的是str迭代器后面的位置,而不是s
·
1.vector
#include <vector> std::vector<int> str; // 插入到末尾 str.push_back(111); str.push_back(222); str.push_back(333); CCLOG("-------------------------"); // 注意i是一个返回的迭代器,str.end()[0]指向的是str迭代器后面的位置,而不是str最后的元素 for(vector<int>::iterator i = str.begin(); i<str.end(); i++) CCLOG("%d",i[0]); //删除元素 str.erase(str.end() - 1); for(vector<int>::iterator i = str.begin(); i<str.end(); i++) CCLOG("%d",i[0]); CCLOG("-------------------------");
输出:
-------------------------
111
222
333
111
222
-------------------------
2、CCArray
》CCArray的好处比较好用,自动释放废弃元素内存,比较符合java程序员的程序员//ccarray CCArray * nodes ; //注意CCArray里面的元素必须继承CCObject CCNode *node = new CCNode(); //插入 nodes->addObject(node); //取出 unsigned int key = 0; //key start form 0 CCNode *temp = (CCNode *)nodes->objectAtIndex(key); //删除 nodes->removeObject(temp);
3、map
#include <map> //注意,STL头文件没有扩展名.h //定义 std::map<int, CCNode *> mapRecord; //插入 mapRecord.insert(map<int, CCNode *> :: value_type(key, value)); //取出,先find一下看有没有,直接用mapRecord[1]如果没有会自动插入一个空的条目 CCNode *node; //定义一个条目变量(实际是指针) std::map<int, CCNode *> ::iterator it= mapRecord.find(key); if(it == mapRecord.end()) { //没找到 node = NULL; } else { //找到 node = it->second; } //删除 /* iterator erase(iterator it); //通过一个条目对象删除 iterator erase(iterator first, iterator last); //删除一个范围 size_type erase(const Key& key); //通过关键字删除 clear()就相当于 enumMap.erase(enumMap.begin(), enumMap.end());*/ std::map<int, CCNode *>::iterator iter = mapRecord.find(key); if (iter != mapRecord.end()) { mapRecord.erase(iter); return true; } else { return false; }
map这里有个博客讲的比较全 :http://wmnmtm.blog.163.com/blog/static/38245714201072310131130/
更多推荐
已为社区贡献1条内容
所有评论(0)