C ++ from the perspective of assembly (generic programming)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Generic programming is not difficult. Essentially, generic programming is to apply general algorithms to all data types. Specifically, int is a familiar Integer type. In general, how should we write an int integer sorting algorithm? Can you try it by yourself? The following code is an example of mine;

void bubble_sort(int array[], int length){int temp = 0;int outer = 0;int inner = 0;assert(NULL != array && 0 != length);for(outer = length -1; outer >= 1; outer --){for(inner = 0; inner <= outer; inner ++){if(array[inner] > array[inner + 1]){temp = array[inner];array[inner] = array[inner + 1];array[inner + 1] = temp;}}}return;}

What do you need to do if you change the data type to a common data type? Two: (1) arithmetic operator "=" overload; (2) Comparison function. The following is a designed class type.

class type{int data;public:type(int value = 0): data(value) {}type(type& t) {data = t.get_data();}~type() {}type& operator=(type& t) {data = t.get_data(); return *this;}int get_data() {return data;}};

So what about the comparison function? We can use a global function instead.

int type_compare(type& t1, type& t2){return t1.get_data() > t2.get_data() ? 1 : 0;}

So far, all the functions have been modified, so the bubble_sort function should also be modified. Let's see how to do it?

template <typename data>void bubble_sort(data array[], int length, int (*compare)(data& , data& )){data temp;int outer = 0;int inner = 0;assert(NULL != array && 0 != length);for(outer = length -1; outer >= 1; outer --){for(inner = 0; inner <= outer; inner ++){if(compare(array[inner], array[inner+1])){temp = array[inner];array[inner] = array[inner + 1];array[inner + 1] = temp;}}}return;}

As you can see, the code is ready for use. Let's see how to use it. Let's look at the following code:

272:      type t[2] = {type(2), type(1)};0040148D   push        20040148F   lea         ecx,[ebp-14h]00401492   call        @ILT+25(type::type) (0040101e)00401497   mov         dword ptr [ebp-4],00040149E   push        1004014A0   lea         ecx,[ebp-10h]004014A3   call        @ILT+25(type::type) (0040101e)004014A8   mov         byte ptr [ebp-4],1273:      bubble_sort<type> (t, 2, type_compare);004014AC   push        offset @ILT+20(type_compare) (00401019)004014B1   push        2004014B3   lea         eax,[ebp-14h]004014B6   push        eax004014B7   call        @ILT+50(bubble_sort) (00401037)004014BC   add         esp,0Ch274:      return;004014BF   mov         byte ptr [ebp-4],0004014C3   lea         ecx,[ebp-10h]004014C6   call        @ILT+5(type::~type) (0040100a)004014CB   mov         dword ptr [ebp-4],0FFFFFFFFh004014D2   lea         ecx,[ebp-14h]004014D5   call        @ILT+5(type::~type) (0040100a)275:  }

We can see that the simple sorting has been completed, and the function will eventually call the bubble_sort function. Although generics are complex and involve knowledge about function pointers, Arithmetic Operators, and template functions, they become more convenient and easier to use as long as they are brave enough to try.

Problem:
(1) Can you try to compile an insert_sort generic function?
(2) try to write a binary generic processing function?
(3) How many factors may be considered when I try to compile a quick_sort generic function? But you can try it.

[Notice: The following blog will write some articles on class skills]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.