黑马程序员--数组常用排序方法

本文通过实例展示了如何使用Objective-C实现数组排序,并详细解释了排序方法的实现原理及不同排序策略的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


接口文件

//
//  Person.h
//  字符串练习
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject


@property (nonatomic,assign) NSString *name;
@property (nonatomic,assign) int record;
@property (nonatomic,assign) int age;


-(Person *)initWithName:(NSString *)name andRecord:(int)record andAge:(int)age;
-(NSComparisonResult) compareName:(Person *)element;
@end

实现文件

//
//  Person.m
//  字符串练习
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import "Person.h"

@implementation Person



@synthesize name;
@synthesize record;
@synthesize age;
//便利构造器
-(Person *)initWithName:(NSString *)name andRecord:(int)record andAge:(int)age
{
    if (self=[super init])
    {
        self->name=name;
        self->age=age;
        self->record=record;
    }
    return self;
    
}
//重写打印方法
-(NSString *)description
{
    return [NSString stringWithFormat:@"%@ %d %d",name,age,record];
}
//排序:只能排序字符串
-(NSComparisonResult) compareName:(Person *)element
{
    return [name compare: element.name];
}
@end


测试文件

//
//  main.m
//  练习多种方式数组排序
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
      
        Person *p1=[[Person alloc] initWithName:@"zhang" andRecord:56 andAge:17];
        
        Person *p2=[[Person alloc] initWithName:@"gao" andRecord:78 andAge:19];
        
        Person *p3=[[Person alloc] initWithName:@"gao" andRecord:56 andAge:34];
        
        Person *p4=[[Person alloc] initWithName:@"sun" andRecord:90 andAge:45];
        Person *p5=[[Person alloc] initWithName:@"gao" andRecord:90 andAge:34];
        
        
        NSArray *arry=[NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
  
        
        /****************按照描述符排序*******************/
        NSLog(@"*************************************");
        //排序描述符
        NSSortDescriptor *nameDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];//升序
        NSSortDescriptor *ageDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//年龄降序
        NSSortDescriptor *recordDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"record" ascending:YES];
        
        
        //把需要排序的描述符放到数组中
        
        NSArray *descriptorArr=[NSArray arrayWithObjects:nameDescriptor,ageDescriptor,recordDescriptor,nil];
        
        NSArray *sortedarr=[arry sortedArrayUsingDescriptors:descriptorArr];
    
        for(Person *element in sortedarr)
        {
            NSLog(@"%@",element);

        }
        
        /****************按照selector*******************/
        NSLog(@"*************************************");
        NSArray *selecotArr=[arry sortedArrayUsingSelector:@selector(compareName:)];
        
        for(Person *element in selecotArr)
        {
            NSLog(@"%@",element);
            
        }
        
        /****************按照代码块排序*******************/
        NSLog(@"*************************************");
        NSArray *blockArr=[arry sortedArrayUsingComparator:^(id obj1,id obj2){return [obj1 compareName:obj2];}];
        
        for(Person *element in blockArr)
        {
            NSLog(@"%@",element);
            
        }
        
    }
    return 0;
}

输出:

2015-06-12 17:23:42.266 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] zhang 17 56

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] zhang 17 56

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.276 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.277 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.277 练习多种方式数组排序[919:35712] zhang 17 56






### 黑马程序员 C++ STL 教程讲解内容 STL(Standard Template Library),即标准模板库,是C++中的一个重要组成部分。它提供了许多高效的算法和数据结构,使得开发者可以更高效地编写程序[^1]。 #### 1. 容器(Container) 容器是用来存储对象的数据结构。STL 提供了多种类型的容器,每种容器都有其特定的应用场景。常见的容器有: - **Sequence Containers (序列容器)** 序列容器按照线性顺序保存元素。主要包括 `vector` 和 `list` 等。 - `vector`: 动态数组,支持随机访问,但在中间插入或删除效率较低[^2]。 ```cpp std::vector<int> vec; vec.push_back(10); ``` - `list`: 双向链表,适合频繁的插入和删除操作,但不支持随机访问[^3]。 ```cpp std::list<int> lst; lst.push_back(20); ``` - **Associative Containers (关联容器)** 关联容器通过键值对来管理数据,查找速度非常快。例如 `map` 和 `set`。 - `map`: 键值映射关系,按键排序。 ```cpp std::map<std::string, int> m; m["apple"] = 5; ``` - `set`: 存储唯一元素,并按一定顺序排列。 ```cpp std::set<int> s; s.insert(7); ``` #### 2. 迭代器(Iterator) 迭代器用于遍历容器中的元素,类似于指针的功能。不同容器有不同的迭代器类型,但它们的操作方式基本一致。 ```cpp std::vector<int> v = {1, 2, 3}; for(auto it = v.begin(); it != v.end(); ++it){ std::cout << *it << " "; } ``` #### 3. 算法(Algorithm) STL 提供了大量的泛型算法,这些算法可以直接作用于不同的容器上。常用的算法包括但不限于: - 排序:`sort`, `stable_sort` - 查找:`find`, `binary_search` - 修改:`copy`, `transform` - 数学运算:`accumulate`, `inner_product` 示例代码展示如何使用 `sort` 对 `vector` 中的元素进行排序: ```cpp #include <algorithm> #include <vector> int main(){ std::vector<int> data = {4, 2, 9, 1}; std::sort(data.begin(), data.end()); return 0; } ``` #### 4. 函数对象(Function Object) 函数对象也被称为仿函数,是一种可以通过调用操作符 `()` 使用的对象。它可以像普通函数一样被调用,同时还可以拥有自己的状态。 ```cpp struct Add { int operator()(int a, int b) const{ return a + b; } }; Add adder; int result = adder(3, 5); // 结果为8 ``` #### 5. 模板(Template) 模板技术允许定义通用的类和函数形式参数化类型。这是实现 STL 的核心机制之一[^4]。 - **函数模板** ```cpp template<typename T> void swap(T& a, T& b){ T temp = a; a = b; b = temp; } ``` - **类模板** ```cpp template<class Type> class Stack { // 类体... }; ``` --- ### 总结 黑马程序员在教授 C++ STL 部分时注重理论与实践相结合的方式,帮助学生掌握常用容器、迭代器、算法以及模板等内容。这不仅有助于理解底层原理,还能提高实际开发能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值