Arrays and strings of data structures

Source: Internet
Author: User

One, array

(Title and Procedure from "sword-pointing offer")

An array can be said to be the simplest form of data structure, which occupies a contiguous amount of memory and is stored in order. When creating an array we need to know the size of the array and then allocate memory based on the size.

Because no matter how many elements, the array will be stored for the entire data budget, so the space efficiency is not high, but because the memory in the array is continuous, you can read and write any element in O (1) time according to the subscript, the time is very efficient.

Arrays and pointers are two concepts that are interconnected, interrelated, and differentiated. It is important to note that when you use pointers to access elements in a database, you need to ensure that the bounds of the array are not exceeded.


Example: Finding in a two-dimensional array

Void testdemo () {    int matrix[][4] = {{1, 2, 8, 9},  {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};     test ((int*) matrix, 4, 4, 7, true);} Void test (Char* testname, int* matrix, int rows, int columns, int  number, bool expected) {   find (matrix, rows, columns, number);} Bool find (Int* matrix, int rows, int columns, int number) {     bool found = false;    if (matrix != null & & rows > 0 && columns > 0)     {         int row = 0;         int column = columns - 1;        while (row < rows &&  column >=0)         {             if (Matrix[row * columns + column] == number)              {                 found = true;                 break;             }             Else if (Matrix[row * columns + column] > number)                  -- column;             else                 ++ row;        }    }     return found;}

Second, string

(Title and Procedure from "sword-pointing offer")

Each string in C + + is terminated with '/', so you can find the end of the string according to this feature, as well as additional overhead, so you need to be aware of the problem of string cross-border.

In order to save memory, C + + places the constant string in a separate memory area.

Char str1[]= "Hello World", char str2[]= "Hello World", char* str3= "Hello World", char* str4= "Hello World";

STR1 and str2 are two string arrays, and are two different arrays of initial addresses, so the values for str1 and str2 are not the same.

STR3 and STR4 are two pointers without allocating memory for them to hold data. STR3 and STR4 point to the same address. So the comparison between STR3 and STR4 's worth of results is the same.

String str= "Hello"; str. ToUpper (); Str. Insert (0, "World");

Although the above operation is performed on STR, the result of the operation is returned in a new string instance and return value, and the value in STR itself does not change. Continuous modification with string, each time a modification produces a temporary object, the overhead is too high. So there are StringBuilder in C # and Java to accommodate the modified results.


Example: replacing spaces

Void replaceblank (char string[], int length) {    if (string ==  null && length <= 0)          return;    /*originallength  is the actual length of string strings */    int  originallength = 0;    int numberofblank = 0;     int i = 0;    while (string[i] !=  ')      {        ++ originalLength;         if (string[i] ==  '   ')              ++ numberOfBlank;        ++ i;     }    /*newLength  the length after replacing the space with '%20 ' */    int  Newlength = originallength&nbsP;+ numberofblank * 2;    if (newlength > length)          return;    int indexOfOriginal =  Originallength;    int indexofnew = newlength;    while (indexoforiginal >= 0 && indexofnew > indexoforiginal)      {        if (string[indexoforiginal] ==  '   ')         {             string[indexOfNew --] =  ' 0 ';             string[indexOfNew --] =  ' 2 ';             string[indexOfNew --] =  '% ';         }        else        {             string[indexofnew --] = string[indexoforiginal] ;        }        --  indexoforiginal;    }}

If you are merging two arrays, the previous copy needs to be moved multiple times, copied from the back, so that you can reduce the number of moves and thus improve efficiency.

This article from "11773640" blog, declined reprint!

Arrays and strings of data structures

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.