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]);
  • 相关阅读:
    Synchronized 锁 批量重偏向 和批量撤销
    Synchronize 偏向锁,轻量级锁升级和撤销过程,对象头的变化
    JAVA 对象到底有什么
    什么是操作系统虚拟地址
    从C角度看 i = i+ 1本质
    Linux操作系统的三种锁机制
    SpringCloud启动过程中如何完成Nacos服务注册
    Nacos源码一
    JAVA线程的本质
    SpringCloud启动-Nacos服务注册
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2011-2022 走看看