Sword refers to offer Reading Notes [1]

Source: Internet
Author: User
1. When defining a value assignment operator, consider the following four points:
  • Whether to declare the type of the returned value as a reference of this type, and return the instance reference (* This) before the function ends ). Only one reference can be returned to allow continuous value assignment. Otherwise, if the return value of the function is void, the value assignment operator cannot be used for continuous value assignment.
  • Whether to declare the input parameter type as a constant reference. If the input parameter is not a reference but an instance, a copy constructor is called from the form parameter to the real parameter. Declaring the parameter as a reference can avoid unnecessary consumption and improve code efficiency. At the same time, the input instance status is not changed within the value assignment function. Therefore, the const keyword should be added before the input reference parameter.
  • Whether to release the existing memory of the instance. If we forget to release the existing space before allocating the new memory, hengxu will have a memory leak.
  • Determines whether the input parameter is the same as the current instance. If it is the same, no value assignment is performed and a value is directly returned. If the value is assigned without prior judgment, serious problems will occur when the instance memory is released, when * This is the same as the input parameter, once the memory of the input parameter is released, the memory of the input parameter will be released at the same time, therefore, the content to be assigned is no longer found.

After fully considering the above four aspects, we can write the following code:

CMyString& CMyString::operator = (const CMyString &str){    if(this==&str)      return;    delete []m_pData;    m_pData=NULL;    m_pData=new char[strlen(str.m_pData)+1];    strcpy(m_pData,str.m_pData);    return *this;}
2. To implement exception security in the value assignment operator function, we have two methods:
  • Method 1: Use new to allocate new content and then delete to release existing content. In this way, only after the allocated content is successfully released, in other words, when the memory allocation fails, we can ensure that the instance of cmystring will not be modified.
  • Method 2: create a temporary instance first, and then exchange the temporary instance with the original Instance.

The following shows the implementation code of the second method:

CMyString& CMyString::operator = (const CMyString &str){    if(this!=&str){      CMyString strTemp(str);      char* pTemp=strTemp.m_pData;      strTemp.m_pData=m_pData;      m_pData=pTemp;  }  return *this}
3. Understanding of struct and class in C ++ and C #
  • C ++: If the access permission level of member functions or member variables is not specified in C ++, the default access permission level is public in struct and private in class.
  • C #: in C #, if the access level of a member function or member variable is not specified, both struct and class are private by default. The difference is that struct defines the value type, its instance allocates memory on the stack. class defines the reference type, and its instance allocates memory on the stack.
4. Implement Singleton mode in C #
  • Principle: in the C # syntax, C # initializes static variables when calling static functions ,.. Net can ensure that only one static constructor is called, so that we can ensure that only one instance is initialized;
    The following is a sample code:
public sealed class Singleton{    private Singleton()  {  }  private static Singleton instance=new Singleton();  public static Singleton Instance  {    get{ return instance;}  }}
5. Important Concepts of C ++ Array
  • Array is the simplest data structure. It occupies a continuous memory and stores data in sequence.
  • In C/C ++, arrays and pointers are interrelated and different concepts. When we declare an array, its array name is a pointer at the same time, which points to the first element of the array, so we can use a pointer to access the array. It is worth noting that C/C ++ does not record the size of the array. Therefore, when using pointers to access elements in the array, note that it cannot exceed the boundary of the array.
  • When sizeof is used to calculate the pointer size, in a 32-bit operating system, the result of any pointer is 4.
  • Two-dimensional arrays occupy continuous space in the memory. Each row of elements is stored from top to bottom in the memory and left to right in the same row. Therefore, we can calculate the offset relative to the first address of the Array Based on the row number and column number to find the corresponding element.
6. string type in C #
  • The sysytem. string type encapsulated in C # has a very special property, that is, the content in the string cannot be changed. When you try to change the content in the string, a new instance will be generated.
  • If you want to modify the string content multiple times in a row, you can use stringbuilder.
  • When we need to return a string instance in a function or method, we need to add the ref or out mark before the input parameter.
7. Linked List
  • A linked list is a dynamic data structure, because you do not need to know the length of the linked list when creating a linked list. When inserting a knot, we only need to allocate memory for the new node, and then adjust the pointer to ensure that the new node is linked to the linked list. The memory allocation is not completed at one time when the linked list is created, but is allocated every time a node is added. Because there is no idle memory, the linked list space efficiency is higher than the array.
  • Because the memory in the linked list is not allocated at one time, we cannot determine that the memory in the linked list is the same as that in the array. Therefore, if you want to find the I node in the linked list, we can only start from the first node and follow the pointer to the next node to traverse the chain table. The efficiency is O (n ). In the array, we can find the I-th element based on the subscript I, and its efficiency is O (1 ).
  • When we need to output the linked list from the end to the end, the first output is the last output of the traversal node, and the first output is the first output of the last traversal node. This is a typical post-first-out process, therefore, we can consider using stacks to implement this sequence. The specific code implementation is as follows:
void PrintListReversingly_Iteratively(ListNode* pHead){    std::stack<ListNode*> nodes;    ListNode* pNode=pHead;    while(pNode!=NULL)    {      nodes.push(pNode);      pNode=pNode->m_pNext;    }    while(!nodes.empty())    {      pNode=nodes.top();      printf("%d\t",pNode->m_nValue);      nodes.pop();    }}
8. Tree
  • Each node except the root node has only one parent node, and the root node has no parent node.
  • All nodes except the leaf node have one or more sub-nodes, and the leaf node does not have any sub-nodes. Use a pointer to link the Parent and Child Nodes.
  • A binary tree is a special structure of a tree. Each node of a binary tree can have at most two subnodes. There are three main ways to traverse a binary tree: Forward traversal (root, left, right), and Central traversal (left, root, right) and backward traversal (left, right, and root ).
  • The binary search tree is a special case of a binary tree. It features that the Left subnode is always smaller than or equal to the root node, and the right subnode is always greater than or equal to the root node.
9. Stack and queue
  • Stack is characterized by the following: the last push element is the first pop ).
  • The queue is characterized by first-in-first-out, that is, the first element to enter the queue (into the queue) will be first (out of the queue ).
10. recursion and loop
  • The efficiency of recursive implementation cannot be compared with that of a loop. Therefore, function calling may cause loss of time and space, repeated computation, and stack overflow.
    In the classic Fibonacci series, we can use the following method to replace the traditional recursive method:
int Fiboncci(int n){  int[] result=new int[]{0,1};  if(n<2)     return result[n];  int m=1;  int n=0;  int k=0;  for(int i=2;i<=n;i++)  {     k=m+n;     n=m;     m=k;  }  return k;}
11. bitwise operations


  • Move left m <

Sword refers to offer Reading Notes [1]

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.