写博客只是为了让自己学的更深刻,参考:https://tech.meituan.com/DiveIntoCategory.html

分类(Category)是个啥玩意儿这里就不多介绍了,这里主要是研究下,分类的底层实现。

1. 分类中为什么不能添加成员变量?

在Objective-C提供的runtime函数中,确实有一个class_addIvar()函数用于给类添加成员变量,但是文档中特别说明:

  1. This function may only be called after objc_allocateClassPair and before objc_registerClassPair. Adding an instance variable to an existing class is not supported.

意思是说,这个函数只能在“构建一个类的过程中”调用。一旦完成类定义,就不能再添加成员变量了。经过编译的类在程序启动后就runtime加载,没有机会调用addIvar。程序在运行时动态构建的类需要在调用 objc_allocateClassPair 之后,objc_registerClassPair之前才可以被使用,同样没有机会再添加成员变量。那为什么可以在类别中添加方法和属性呢?
因为方法和属性并不“属于”类实例,而成员变量“属于”类实例。我们所说的“类实例”概念,指的是一块内存区域,包含了isa指针和所有的成员变量。所以假如允许动态修改类成员变量布局,已经创建出的类实例就不符合类定义了,变成了无效对象。但方法定义是在objc_class中管理的,不管如何增删类方法,都不影响类实例的内存布局,已经创建出的类实例仍然可正常使用。

2. Category 和 Extension(类扩展)

Extension是Category的一个实例,被称为匿名分类,可以为一个类添加一些私有变量和方法。但是Extension和有名字的Category完全是两个东西。

Extension在编译期决定,它就是类的一部分,在编译期和头文件里的@interface和实现文件里的@implement一起形成一个完整的类,它伴随类的产生而产生,消亡而消亡。一般用来隐藏类的私有信息,你必须有一个类的源码才能添加Extension,比如 NSString 系统类就无法添加。

Category是在运行期决定的,无法添加成员变量,因为在运行期间,对象的内存布局已经确定,如果添加实例变量就会破坏类的内存布局。

3. Category 结构

所有的对象和类,在runtime层都是由struct表示的,category 也是如此,我们可以下载 runtime 的代码,objc-runtime-new.h 可以看到,category 使用 category_t 进行表示的:

  1. typedef struct category_t {
  2. const char *name; // 类的名字
  3. classref_t cls; // 类
  4. struct method_list_t *instanceMethods; // 实例方法
  5. struct method_list_t *classMethods; // 类方法
  6. struct protocol_list_t *protocols; // 协议
  7. struct property_list_t *instanceProperties; // 所有属性
  8. } category_t;

下面我新建了一个项目,给NSObject添加了一个分类:

  1. @interface MyClass : NSObject
  2.  
  3. - (void)printName;
  4.  
  5. @end
  6.  
  7. @interface MyClass(MyAddition)
  8.  
  9. @property(nonatomic, copy) NSString *myName;
  10.  
  11. - (void)printName;
  12.  
  13. @end
  14.  
  15. @implementation MyClass(MyAddition)
  16.  
  17. - (void)printName{
  18. NSLog(@"MyAddition");
  19. }
  20.  
  21. @end
  22.  
  23. @implementation MyClass
  24.  
  25. - (void)printName
  26. {
  27. NSLog(@"MyClass");
  28. }
  29.  
  30. @end

然后使用 clang 命令 :clang -rewirte-objc MyClass.m,生成 .cpp 文件,打开查看,在文件的最下方:

  1. // 首先生成了 _OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition 实例方法列表,和 _OBJC_$_PROP_LIST_MyClass_$_MyAddition 属性列表。遵循命名方法:公共前缀+类名+Category
    注意:category的名字用来给各种列表以及后面的category结构体本身命名,而且有static来修饰,所以同一个编译单元(文件),不能有两个相同名字的category,否则会报编译错误。
      
    static struct /*_method_list_t*/ {// 实例方法列表
  2. unsigned int entsize; // sizeof(struct _objc_method)
  3. unsigned int method_count;
  4. struct _objc_method method_list[1];
  5. } _OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition __attribute__ ((used, section ("__DATA,__objc_const"))) = {
  6. sizeof(_objc_method),
  7. 1,
  8. {{(struct objc_selector *)"printName", "v16@0:8", (void *)_I_MyClass_MyAddition_printName}}
  9. };
  10.  
  11. static struct /*_prop_list_t*/ { // 属性列表
  12. unsigned int entsize; // sizeof(struct _prop_t)
  13. unsigned int count_of_properties;
  14. struct _prop_t prop_list[1];
  15. } _OBJC_$_PROP_LIST_MyClass_$_MyAddition __attribute__ ((used, section ("__DATA,__objc_const"))) = {
  16. sizeof(_prop_t),
  17. 1,
  18. {{"myName","T@\"NSString\",C,N"}}
  19. };
  20.  
  21. extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_MyClass;

  22. // 然后,生成了Category 本身, _OBJC_$_CATEGORY_MyClass_$_MyAddition ,并用上一步生成的 实例方法列表 和 属性列表 来初始化Category本身。
  23. static struct _category_t _OBJC_$_CATEGORY_MyClass_$_MyAddition __attribute__ ((used, section ("__DATA,__objc_const"))) =
  24. {
  25. "MyClass",
  26. 0, // &OBJC_CLASS_$_MyClass,
  27. (const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition,
  28. 0,
  29. 0,
  30. (const struct _prop_list_t *)&_OBJC_$_PROP_LIST_MyClass_$_MyAddition,
  31. };
  32. static void OBJC_CATEGORY_SETUP_$_MyClass_$_MyAddition(void ) {
  33. _OBJC_$_CATEGORY_MyClass_$_MyAddition.cls = &OBJC_CLASS_$_MyClass;
  34. }
  35. #pragma section(".objc_inithooks$B", long, read, write)
  36. __declspec(allocate(".objc_inithooks$B")) static void *OBJC_CATEGORY_SETUP[] = {
  37. (void *)&OBJC_CATEGORY_SETUP_$_MyClass_$_MyAddition,
  38. };
  39. static struct _class_t *L_OBJC_LABEL_CLASS_$ [1] __attribute__((used, section ("__DATA, __objc_classlist,regular,no_dead_strip")))= {
  40. &OBJC_CLASS_$_MyClass,
  41. };
    // 最终,编译器在DATA段下的 objc_catlistsection 里,保存了一个大小为1的 category_t 数组,里面那个就是我们刚才生成的Category,用于运行期Category的加载。
  42. static struct _category_t *L_OBJC_LABEL_CATEGORY_$ [1] __attribute__((used, section ("__DATA, __objc_catlist,regular,no_dead_strip")))= {
  43. &_OBJC_$_CATEGORY_MyClass_$_MyAddition,
  44. };
  45. static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };

编译期到此结束,下面我们看一下,是如何加载的。

4. Category 如何加载?

对于OC运行时,入口方法如下:objc-os.mm文件中

  1. void _objc_init(void)
  2. {
  3. static bool initialized = false;
  4. if (initialized) return;
  5. initialized = true;
  6.  
  7. // fixme defer initialization until an objc-using image is found?
  8. environ_init();
  9. tls_init();
  10. static_init();
  11. lock_init();
  12. exception_init();
  13.  
  14. _dyld_objc_notify_register(&map_images, load_images, unmap_image);
  15. }

Category 被附加到类上是在 map_images 的时候发生的(我们可以点进去看到),_objc_init里面的调用的map_images最终会调用objc-runtime-new.mm里面的_read_images方法,而在_read_images方法的结尾,有以下的代码片段:

  1. // Discover categories.
  2. for (EACH_HEADER) {
         // 我们这里拿到的catlist 就是上面编译期间我们生成的category_t数组
  3. category_t **catlist =
  4. _getObjc2CategoryList(hi, &count);
  5. bool hasClassProperties = hi->info()->hasCategoryClassProperties();
  6.  
  7. for (i = 0; i < count; i++) {
  8. category_t *cat = catlist[i];
  9. Class cls = remapClass(cat->cls);
  10.  
  11. if (!cls) {
  12. // Category's target class is missing (probably weak-linked).
  13. // Disavow any knowledge of this category.
  14. catlist[i] = nil;
  15. if (PrintConnecting) {
  16. _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with "
  17. "missing weak-linked target class",
  18. cat->name, cat);
  19. }
  20. continue;
  21. }
  22.  
  23. // Process this category.
  24. // First, register the category with its target class.
  25. // Then, rebuild the class's method lists (etc) if
  26. // the class is realized.
  27. bool classExists = NO;
  28. if (cat->instanceMethods || cat->protocols
  29. || cat->instanceProperties)
  30. {
             // 获取到实例方法列表之后,下面这个方法只是将类和category进行一个关联
  31. addUnattachedCategoryForClass(cat, cls, hi);
  32. if (cls->isRealized()) {
                // 最主要的实现代码是在这个方法中
  33. remethodizeClass(cls);
  34. classExists = YES;
  35. }
  36. if (PrintConnecting) {
  37. _objc_inform("CLASS: found category -%s(%s) %s",
  38. cls->nameForLogging(), cat->name,
  39. classExists ? "on existing class" : "");
  40. }
  41. }
  42.  
  43. if (cat->classMethods || cat->protocols
  44. || (hasClassProperties && cat->_classProperties))
  45. {
  46. addUnattachedCategoryForClass(cat, cls->ISA(), hi);
  47. if (cls->ISA()->isRealized()) {
  48. remethodizeClass(cls->ISA());
  49. }
  50. if (PrintConnecting) {
  51. _objc_inform("CLASS: found category +%s(%s)",
  52. cls->nameForLogging(), cat->name);
  53. }
  54. }
  55. }
  56. }

略去PrintConnecting这个用于log的东西,这段代码很容易理解:
1)、把category的实例方法、协议以及属性添加到类上
2)、把category的类方法和协议添加到类的metaclass上

下面我们去探究下真正处理添加事宜的 remethodizeClass 方法:

  1. static void remethodizeClass(Class cls)
  2. {
  3. category_list *cats;
  4. bool isMeta;
  5.  
  6. runtimeLock.assertWriting();
  7.  
  8. isMeta = cls->isMetaClass();
  9.  
  10. // Re-methodizing: check for more categories
  11. if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) {
  12. if (PrintConnecting) {
  13. _objc_inform("CLASS: attaching categories to class '%s' %s",
  14. cls->nameForLogging(), isMeta ? "(meta)" : "");
  15. }
  16.  
  17. attachCategories(cls, cats, true /*flush caches*/);
  18. free(cats);
  19. }
  20. }

好吧,这个方法其实又会去调用attachCategories这个方法,我们去看下attachCategories:

  1. static void
  2. attachCategories(Class cls, category_list *cats, bool flush_caches)
  3. {
  4. if (!cats) return;
  5. if (PrintReplacedMethods) printReplacements(cls, cats);
  6.  
  7. bool isMeta = cls->isMetaClass();
  8.  
  9. // fixme rearrange to remove these intermediate allocations
  10. method_list_t **mlists = (method_list_t **)
  11. malloc(cats->count * sizeof(*mlists));
  12. property_list_t **proplists = (property_list_t **)
  13. malloc(cats->count * sizeof(*proplists));
  14. protocol_list_t **protolists = (protocol_list_t **)
  15. malloc(cats->count * sizeof(*protolists));
  16.  
  17. // Count backwards through cats to get newest categories first
  18. int mcount = 0;
  19. int propcount = 0;
  20. int protocount = 0;
  21. int i = cats->count;
  22. bool fromBundle = NO;
  23. while (i--) {
  24. auto& entry = cats->list[i];
  25.  
  26. method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
  27. if (mlist) {
  28. mlists[mcount++] = mlist;
  29. fromBundle |= entry.hi->isBundle();
  30. }
  31.  
  32. property_list_t *proplist =
  33. entry.cat->propertiesForMeta(isMeta, entry.hi);
  34. if (proplist) {
  35. proplists[propcount++] = proplist;
  36. }
  37.  
  38. protocol_list_t *protolist = entry.cat->protocols;
  39. if (protolist) {
  40. protolists[protocount++] = protolist;
  41. }
  42. }
  43.  
  44. auto rw = cls->data();
  45.  
  46. prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
  47. rw->methods.attachLists(mlists, mcount);
  48. free(mlists);
  49. if (flush_caches && mcount > 0) flushCaches(cls);
  50.  
  51. rw->properties.attachLists(proplists, propcount);
  52. free(proplists);
  53.  
  54. rw->protocols.attachLists(protolists, protocount);
  55. free(protolists);
  56. }

这个方法的主要任务是,获取所有该类所有的category,然后通过遍历,将所有category的属性,协议,方法分别放到一个大的数组里,然后通过 attachLists 方法添加:

  1. void attachLists(List* const * addedLists, uint32_t addedCount) {
  2. if (addedCount == 0) return;
  3.  
  4. if (hasArray()) {
  5. // many lists -> many lists
  6. uint32_t oldCount = array()->count;
  7. uint32_t newCount = oldCount + addedCount;
  8. setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
  9. array()->count = newCount;
  10. memmove(array()->lists + addedCount, array()->lists,
  11. oldCount * sizeof(array()->lists[0]));
  12. memcpy(array()->lists, addedLists,
  13. addedCount * sizeof(array()->lists[0]));
  14. }
  15. else if (!list && addedCount == 1) {
  16. // 0 lists -> 1 list
  17. list = addedLists[0];
  18. }
  19. else {
  20. // 1 list -> many lists
  21. List* oldList = list;
  22. uint32_t oldCount = oldList ? 1 : 0;
  23. uint32_t newCount = oldCount + addedCount;
  24. setArray((array_t *)malloc(array_t::byteSize(newCount)));
  25. array()->count = newCount;
  26. if (oldList) array()->lists[addedCount] = oldList;
  27. memcpy(array()->lists, addedLists,
  28. addedCount * sizeof(array()->lists[0]));
  29. }
  30. } 

需要注意的有两点:
1)、category的方法没有“完全替换掉”原来类已经有的方法,也就是说如果category和原来类都有methodA,那么category附加完成之后,类的方法列表里会有两个methodA
2)、category的方法被放到了新方法列表的前面,而原来类的方法被放到了新方法列表的后面,这也就是我们平常所说的category的方法会“覆盖”掉原来类的同名方法,这是因为运行时在查找方法的时候是顺着方法列表的顺序查找的,它只要一找到对应名字的方法,就会罢休,殊不知后面可能还有一样名字的方法。

下面我们也会来验证一下:

5. 如何调用到被覆盖的主类的方法?

我们已经知道category其实并不是完全替换掉原来类的同名方法,只是category在方法列表的前面而已,所以我们只要顺着方法列表找到最后一个对应名字的方法,就可以调用原来类的方法:

  1. Class currentClass = [MyClass class];
  2. MyClass *my = [[MyClass alloc] init];
  3.  
  4. if (currentClass) {
  5. unsigned int methodCount;
  6. Method *methodList = class_copyMethodList(currentClass, &methodCount);
  7. IMP lastImp = NULL;
  8. SEL lastSel = NULL;
  9. for (NSInteger i = 0; i < methodCount; i++) {
  10. Method method = methodList[i];
  11. NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(method))
  12. encoding:NSUTF8StringEncoding];
  13. if ([@"printName" isEqualToString:methodName]) {
  14. lastImp = method_getImplementation(method);
  15. lastSel = method_getName(method);
  16. }
  17. }
  18. typedef void (*fn)(id,SEL);
  19.  
  20. if (lastImp != NULL) {
  21. fn f = (fn)lastImp;
  22. f(my,lastSel);
  23. }
  24. free(methodList);
  25. }

6. 为分类添加属性,关联对象

如上所见,我们知道在category里面是无法为category添加实例变量的。但是我们很多时候需要在category中添加和对象关联的值,这个时候可以求助关联对象来实现。

  1. - (void)setMyName:(NSString *)myName{
  2. objc_setAssociatedObject(self, @"myName", myName, OBJC_ASSOCIATION_COPY);
  3. }
  4.  
  5. -(NSString *)myName{
  6. return objc_getAssociatedObject(self, @"myName");
  7. }

这里就不多做介绍了,我们来看下,是如何关联的,去翻一下runtime的源码,在objc-references.mm文件中有个方法_object_set_associative_reference:

  1. void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
  2. // retain the new value (if any) outside the lock.
  3. ObjcAssociation old_association(0, nil);
  4. id new_value = value ? acquireValue(value, policy) : nil;
  5. {
  6. AssociationsManager manager;
  7. AssociationsHashMap &associations(manager.associations());
  8. disguised_ptr_t disguised_object = DISGUISE(object);
  9. if (new_value) {
  10. // break any existing association.
  11. AssociationsHashMap::iterator i = associations.find(disguised_object);
  12. if (i != associations.end()) {
  13. // secondary table exists
  14. ObjectAssociationMap *refs = i->second;
  15. ObjectAssociationMap::iterator j = refs->find(key);
  16. if (j != refs->end()) {
  17. old_association = j->second;
  18. j->second = ObjcAssociation(policy, new_value);
  19. } else {
  20. (*refs)[key] = ObjcAssociation(policy, new_value);
  21. }
  22. } else {
  23. // create the new association (first time).
  24. ObjectAssociationMap *refs = new ObjectAssociationMap;
  25. associations[disguised_object] = refs;
  26. (*refs)[key] = ObjcAssociation(policy, new_value);
  27. object->setHasAssociatedObjects();
  28. }
  29. } else {
  30. // setting the association to nil breaks the association.
  31. AssociationsHashMap::iterator i = associations.find(disguised_object);
  32. if (i != associations.end()) {
  33. ObjectAssociationMap *refs = i->second;
  34. ObjectAssociationMap::iterator j = refs->find(key);
  35. if (j != refs->end()) {
  36. old_association = j->second;
  37. refs->erase(j);
  38. }
  39. }
  40. }
  41. }
  42. // release the old value (outside of the lock).
  43. if (old_association.hasValue()) ReleaseValue()(old_association);
  44. }

我们可以看到,所有的关联对象都是由AssociationsManager 管理,而AssociationsManager的定义如下:

  1. class AssociationsManager {
  2. // associative references: object pointer -> PtrPtrHashMap.
  3. static AssociationsHashMap *_map;
  4. public:
  5. AssociationsManager() { AssociationsManagerLock.lock(); }
  6. ~AssociationsManager() { AssociationsManagerLock.unlock(); }
  7.  
  8. AssociationsHashMap &associations() {
  9. if (_map == NULL)
  10. _map = new AssociationsHashMap();
  11. return *_map;
  12. }
  13. };

AssociationsManager里面有一个静态的 AssociationHashMap 来存储所有的关联对象,这相当于把所有对象的关联对象都放到了一个map里,而map的key是对象的指针地址,而这个map的value又是另外一个 AssociationsHasMap, 里面保存的是关联对象的kv对。

在对象的销毁方法里面,见objc-runtime-new.mm:

  1. void *objc_destructInstance(id obj)
  2. {
  3. if (obj) {
  4. // Read all of the flags at once for performance.
  5. bool cxx = obj->hasCxxDtor();
  6. bool assoc = obj->hasAssociatedObjects();
  7.  
  8. // This order is important.
  9. if (cxx) object_cxxDestruct(obj);
  10. // 这里判断是否有关联对象,有的话就去清理,所以不用自己去清理
  11. if (assoc) _object_remove_assocations(obj);
  12. obj->clearDeallocating();
  13. }
  14.  
  15. return obj;
  16. }

Runtime - ③ - 分类Category探究的更多相关文章

  1. OC中分类(Category)和扩展(Extension)

    1.分类的定义 category是Objective-C 2.0之后添加的语言特性,中文也有人称之为分类.类别.Category的主要作用是为已经存在的类添加方法.这个大家可能用过很多,如自己给UIC ...

  2. 分类(Category)的本质 及其与类扩展(Extension) /继承(Inherit)的区别

    1.分类的概念 分类是为了扩展系统类的方法而产生的一种方式,其作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法. 如何创建一个分类?↓↓ ()Cmd+N, ...

  3. 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...

  4. 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol

    关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol 2014-02-18 19:57 315人阅读 评论(0) 收藏 举报 1.category, ...

  5. Objective-C分类 (category)和扩展(Extension)

    1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...

  6. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  7. iOS之分类(category)

    1.分类(category)的作用 1.1作用:可以在不修改原来类的基础上,为一个类扩展方法.1.2最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? 2.1分类中只能添加“方法”,不能增加 ...

  8. Objective-C:继承、分类(Category、extension)、协议(protocol),个人理解,仅供参考

    总结:继承.分类(Category.extension).协议(protocol)   一.继承: (1)特点: 继承多用于一般父类中的方法功能比较齐全,子类从父类继承过来使用,可以省略很多重复的代码 ...

  9. Objective-C:分类(Category、extension)

    分类(Category .Extension) (一)分类的划分     (2) 1.(命名的类别)类别Category:只能添加新的方法,不能添加新变量.           2.(未命名的类别)类 ...

随机推荐

  1. linux常用的压缩与解压缩命令

    1.gzip 压缩 gzip 是压缩文件,压缩之后文件后缀为.gz 用法:gzip 选项 [文件] 2.gunzip 解压 这个命令与gzip的功能刚好相反,这个是解压. 用法 gunzip 选项 [ ...

  2. 学习TensorFlow,浅析MNIST的python代码

    在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...

  3. 小文本——Cookies

    http协议的无状态性导致在需要会话的场景下寸步难行,例如一个网站为了方便用户,在一段时间内登录过改网站的浏览器客户端实现自动登录,为实现这种客户端与服务器之间的会话机制需要额外的一些标识,http头 ...

  4. JAVA之旅(二十八)——File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤

    JAVA之旅(二十八)--File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤 我们可以继续了,今天说下File 一.File概述 文件的操作是非常 ...

  5. SQL Server扫盲系列——安全性专题——SQL Server 2012 Security Cookbook

    由于工作需要,最近研究这本书:<Microsoft SQL Server 2012 Security Cookbook>,为了总结及分享给有需要的人,所以把译文公布.预计每周最少3篇.如有 ...

  6. 常用Petri网模拟软件工具简介

    常用Petri网模拟软件工具简介 首先要介绍的的一个非常有名的Petri 网网站--Petri Nets World:       http://www.informatik.uni-hamburg. ...

  7. hello 内核模块

    #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #include <lin ...

  8. python发送post请求

    urllib2.urlopen() urlib2是使用各种协议完成打开url的一个扩展包.最简单的使用方式是调用urlopen方法,比如 def urlopen(url, data=None, tim ...

  9. 【算法导论】最小生成树之Prime法

    关于最小生成树的概念,在前一篇文章中已经讲到,就不在赘述了.下面介绍Prime算法:         其基本思想为:从一个顶点出发,选择由该顶点出发的最小权值边,并将该边的另一个顶点包含进来,然后找出 ...

  10. C++异常处理 - 栈解旋,异常接口声明,异常类型和异常变量的生命周期

    栈解旋(unwinding) 异常被抛出后,从进入try块起,到异常被抛掷前,这期间在栈上的构造的所有对象,都会被自动析构.析构的顺序与构造的顺序相反.这一过程称为栈的解旋(unwinding). d ...