当C++遇到IOS应用开发---Dict集合
来源:互联网 发布:php刷信誉平台源码 编辑:程序博客网 时间:2024/04/29 18:58
在Object-c中,字典(KEY/VALUE)使用NSDictionary 和NSMutableDictionary(可变长)。使用语法如下:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key1",nil];//注意用nil结束
取元素的方法:
NSString *value = [myDictionary objectForKey:@"key1"];
该写法过于繁复。所以我将C++中的map类进行了封装
#ifndef Discuz2_Maps_h#define Discuz2_Maps_h#include <map> //比较器template <class key_t>struct dict_compare{ bool operator()(key_t x, key_t y){ return x < y;}}; //仅针对NSString*比较器template <>struct dict_compare<NSString*>{ bool operator()(NSString* __x, NSString* __y){ string x = [__x UTF8String]; string y = [__y UTF8String]; //std::cout<<x<< " " <<y<<endl; return x < y;}};#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }#define RETAIN_SAFELY(__POINTER) { [__POINTER retain]; } template <typename K, typename V, typename _Compare = dict_compare<K>,typename _Alloc = std::allocator<std::pair<const K, V> > >class Dict: public map<K, V, _Compare, _Alloc>{private: typedef typename std::map<K, V>::iterator dict_it; public: Dict(){} Dict(NSArray *array){ copyFromArray(array); } ~Dict() { std::cout<<"dict destroy!"<<endl; this->clearAll(); } Dict& add(const K key, const V value){ this->insert(std::make_pair(key, value)); //RETAIN_SAFELY(value); 仅限于OBJECT-C对象类型 return (*this); } const V get(const K key){ dict_it it = this->find(key); if(it != this->end()) return it->second; else return nil; } BOOL contains(const K key){ return this->find(key) == this->end() ? NO: YES; } /* * 返回指定KEY的VALUE */ const V operator[](const K key) { return this->get(key); } void remove(const K key){ this->erase(key); } void clearAll(){ for_each(this->begin(), this->end(), ^(std::pair<K, V> pair){ //RELEASE_SAFELY(pair.second); 仅限于OBJECT-C对象类型 //this->erase(pair.first); });this->clear(); } void copyFromArray(NSArray *array){ if([array count] % 2 == 0){//元素个数需为偶数 for(int i = 0; i< [array count]; i++){ K key = (K)[array objectAtIndex:i]; V value = (V)[array objectAtIndex:++i]; this->add(key, value); } } }};#endif
大家看到,这里使用的定义比较器的方式来指定当KEY的类型是NSString时的比较方式。而这样做的原因是当查找某KEY(NSString类型)时,这时在map比较器中的数据会出现字符长度变长的情况,且变长的那部分内容为乱码。这会造成查不到相应的结果。当然这样做会不会造成性能上的损失,目前还没测试过。感兴趣的朋友可以帮助测试一下。
下面来看一下具体用法。
实例化对象并添加数据:
Dict<NSString*,NSString*> dict; dict.add(@"代震军1", @"111111").add(@"代震军2", @"222222");
或用下面方式:
NSArray *array = [[NSArray alloc] initWithObjects: @"One", @"1", @"Two", @"2", @"Three", @"3", @"Four", @"4",nil]; Dict<NSString*, NSString*> dictBatch(array);
判断是否存在某数据:
BOOL iscontains = dict.contains(@"代震军3");
获取记录条数:
int size = dictBatch.size();
遍历:
for(std::map<NSString*, NSString*>::iterator it = dict.begin(); it != dict.end();it++){ cout << [it->second UTF8String ]<<" " ; }
__block NSString* str; for_each(dict.begin(), dict.end(), ^(std::pair<NSString*, NSString*> pair){ str = pair.first; cout << [pair.second UTF8String ]<<" " ; });
获取指定索引记录:
NSString* result = dict[@"代震军2"];
代码比较简单,就不多做解释了,呵呵。
好了,今天的内容就先到这里了。
原文链接:http://www.cnblogs.com/daizhj/archive/2012/11/09/2762753.html
作者: daizhj, 代震军
微博: http://weibo.com/daizhj
Tags:ios, c++, NSDictionary, NSMutableDictionary, map
- 当C++遇到IOS应用开发---Dict集合
- 当C++遇到IOS应用开发之---List集合
- 当C++遇到IOS应用开发---SQLITE篇
- 当C++遇到IOS应用开发---字符串处理
- 当C++遇到IOS应用开发---SQLITE篇
- 当C++遇到IOS应用开发---LRUCache缓存
- 当C++遇到IOS应用开发---SQLITE篇
- 当C++遇到IOS应用开发—LRUCache缓存
- 当C++遇到IOS应用开发---LRUCache缓存
- iOS开发--当Block、代理、通知遇到女朋友
- 当Android开发遇到菜单
- Objective-c,iOS应用开发随笔
- iOS应用开发(三)Objective-C
- 当Android开发遇到魅族手机
- 当Android开发遇到魅族手机
- IOS开发之路-Objective-C 集合上(笔记)
- IOS开发之路-Objective-C 集合下(实战)
- IOS开发之路-Objective-C 集合下(实战2)
- 古老的Delphi 6调用C#开发的简单WebService
- 截取视频中的音频.
- java web开发之安全事项
- AppWidget开发实例讲解(二)
- python学习入门
- 当C++遇到IOS应用开发---Dict集合
- erlang实现二叉树
- android4.0修改bug系列一 .
- 输入n,用最快的方法求Fibonacci 数列的第n 项
- MVVM
- eclipse 配对括号的颜色改变
- 用标准C编写COM(四)
- Core Animation Example Code
- ctags与taglist的使用