zoukankan      html  css  js  c++  java
  • 排序的NSArray中搜索

    在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能:

     

     
    NSMutableArray *sortedArray = [NSMutableArray arrayWithObjects@"Alice"@"Beth",@"Carol",@"Ellen",nil];
     
    //Where is "Beth"?
    unsigned index = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
                                                                                        CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
                                                                                        (CFStringRef)@"Beth",
                                                                                        (CFComparatorFunction)CFStringCompare,
                                                                                        NULL);
    if (index < [sortedArray count])
    {
             NSLog(@"Beth was found at index %u", index);
    else {
             NSLog(@"Beth was not found (index is beyond the bounds of sortedArray)");
    }
     
    //Where should we insert "Debra"?
    unsigned insertIndex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
                                                                                                CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
                                                                                                (CFStringRef)@"Debra",
                                                                                                (CFComparatorFunction)CFStringCompare,
                                                                                                NULL);
    [sortedArray insertObject:@"Debra" atIndex:insertIndex];
    NSLog([sortedArray description]);
  • 相关阅读:
    新组件或功能怎么依附到已有的架构上
    高内聚、低耦合
    软件质量的定义
    软件架构与组件
    架构设计之拥抱着变化而设计(部分讲义分享)
    组件设计原则之概念篇(四)
    抽象类(接口类)的作用
    类,抽象基类,接口类三者间的区别与联系(C++)
    软件的可变性
    软件设计的复杂度
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2011-2022 走看看