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]);
  • 相关阅读:
    Hznu_0j 1533 计算球体积(水)
    电子警察
    UVA ——利用常量数组
    排序算法
    分解质因数
    几种数
    动态规划
    C. The Football Season (枚举) ( Codeforces Round #592 (Div. 2) )
    Fibonacci前n项和 (矩阵乘)
    2153: D.ly的排队问题 (拓扑排序)(vector , set , priority_queue )
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2011-2022 走看看