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]);
  • 相关阅读:
    Android中Alarm的机制
    String字符串操作--切割,截取,替换,查找,比较,去空格.....
    时间类(时间戳的各种转换成)
    android 常用时间格式转换代码
    Android时间戳与字符串相互转换
    Android时间对话框TimePickerDialog介绍
    一种基于Qt的可伸缩的全异步C/S架构server实现(一) 综述
    C++ overloading contructor
    特征生成
    Atitit.软件仪表盘(2)--vm子系统--资源占用监測
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2011-2022 走看看