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++标准转换运算符(2)
    C++标准转换运算符(1)
    未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于……
    我的第一篇博客
    C语言C语言程序
    C语言基本运算符
    C语言流程控制
    C语言关键字、标识(zhi)符、注释
    msado15.dll版本引发的离奇故障
    mySQL错误: The used table type doesn't support FULLTEXT indexes
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2011-2022 走看看