设为首页 加入收藏

TOP

cocos2dx使用map容器实例(C++)
2015-07-20 17:48:08 来源: 作者: 【 】 浏览:1
Tags:cocos2dx 使用 map 容器 实例
关于map容器 cocos2dx中使用map容器,头文件无须添加, 只要声明命名空间using namespace std;即可
关于map学习资料 学习资料1: http://blog.csdn.net/realxie/article/details/7252662 这是一个很不错的基础实例!赞! 我们在cocos2dx的例如helloworld的int中添加以下两段代码:

    map
  
    pMap ;
    int A[100]={5,2,5,8,9};
    for(int i=0;i<100;i++){
        *(A+i) = i;
        pMap[A+i] = A[i]+1;
    }
    
    map
   
    ::iterator it= pMap.begin(); while(it != pMap.end()) { log("%i",(*(it++)->first)); }
   
  

可以在输出结果,从0,到99! PS说一下,pMap[A+i] = A[i]+1;并没有改变数组中数据的值。

学习资料2: http://blog.csdn.net/lijiaz5033/article/details/5202177
这个帖子更是超级赞,很完美的解释了map的基础用法.

我参考他的帖子,写了一个map,是std::map _map;

我们在cocos2dx的例如helloworld的int中添加以下代码:

    /* define a map */
    std::map
  
    _map;
    /* insert */
    _map.insert( std::map
   
    ::value_type("11", 32.8) ); _map.insert( std::map
    
     ::value_type("12", 33.2) ); _map.insert( std::map
     
      ::value_type("ss", 35.8) ); _map.insert( std::map
      
       ::value_type("nn", 36.4) ); _map.insert( std::map
       
        ::value_type("sss", 37.8) ); _map.insert( std::map
        
         ::value_type("kk", 35.8) ); /* 这个是常用的一种map赋值方法 */ _map["kk2"] = 245.3; /* find by key */ std::map
         
          ::iterator itr; itr = _map.find("kk"); if( itr != _map.end() ) { log("Item: %s found, content: %f",itr->first.c_str(),itr->second); }
         
        
       
      
     
    
   
  
输出结果: Item:kk found,content:35.8
他帖子中提及到的以下几个也很常用,不过我上面并未使用. 也写下来,参考看一下吧.

/* delete item from map 删除item */    
 if( itr != _map.end() )    
 {        
  _map.erase(itr);    
 }      
  /* travel through a map */  
 std::map
  
   ::iterator itr1  =  _map.begin();   
for(  ;  itr1  !=  _map.end();  ++itr1 )    
 {  
    std::cout  << "Item:"  << itr1->first << ", content: " << itr1->second << std::endl;  
 }       
 std::cout  << std::endl;        
/* empty a map  清空map*/   
 _map.clear();      
  



学习资料3: cocos2dx引擎的源码 GUIReader的cpp文件

map也可以用来存放指针哦!在cocos2dx GUIReader的文件中 std::map object_map = GUIReader::getInstance()->getParseObjectMap();
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++普通函数与模板函数以及特化函.. 下一篇POJ 2793 Cactus

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)
·Python超详细一条龙 (2025-12-24 18:49:42)
·【超详细】JDK 下载 (2025-12-24 18:19:32)
·Java_百度百科 (2025-12-24 18:19:29)