C + + foundation 4: array and string

catalogue

1, Array

1. First knowledge of array

2. Variable length array

3. Unknown array length

4. Reading and writing of array elements

5. Multidimensional array

6. Constant array

2, String

1. Array string

2. String length

3. Constant string

4. String operation and attribute acquisition

5.string class

1, Array

1. First knowledge of array

a) A continuous memory that holds a fixed number of objects. Once the application is completed, the number of elements cannot be changed

b) The element type is uncertain: basic data type (int, float, bool, etc.) or structure, class, pointer, enumeration,

int num_array1[5]; //Uninitialized, random values 
int num_array2[5] = {0, 1, 2, 3, 4}; //initialization

2. Variable length array

The array size is fixed and its length is specified by the variable

int len = 1;
while ( len < 10 )
{
    int num_array2[len]; //variable-length array
    cout << "len = " << len;
    cout << ", sizeof(num_array2)) = " 
            << sizeof(num_array2) << endl;
    len ++;
}

Variable length array cannot be initialized. First declare the array and assign values one by one after creation

3. Unknown length array

a) The length is determined by the initialization list

int num_array[ ] = {1, 2, 3, 4}; // the type of num_array is "array of 4 int"

b) For function parameters

float array_sum(float values[], size_t length);
float array_sum(float *values, size_t length);

values is the first address of the array

4. Reading and writing of array elements

int array1[4] = {9,8,7,6};
int array2[4];
array2 = array1; //error! array1 array2 is the first address of the array. If array2 = array1, they will point to the same data. The original data will be lost. The syntax prohibits operation, and the address of array2 cannot be changed
array2[0] = array1[0]; //okay
array2[1] = array1[1]; //okay
array2[2] = array1[2]; //okay
array2[3] = array1[3]; //okay

Index

Value

Address

 

 

p+19

 

p+18

 

p+17

 

p+16

3

6

p+15

p+14

p+13

p+12

2

7

p+11

p+10

p+9

p+8

1

8

p+7

p+6

p+5

p+4

0

9

p+3

p+2

p+1

p+0

 

p-1

 

p-2

 

p-3

 

p-4

 

Note: there is no boundary check for arrays in c + +. If they cross the boundary, they will still operate without warning

int num_array[5]; 

for(int idx = -1; idx <= 5; idx++)
    num_array[idx] = idx * idx;//-1: p-1 array[-1]=1 

for(int idx = -1; idx <= 5; idx++)
    cout << num_array[idx] << endl;

No boundary check, high efficiency

5. Multidimensional array

a)

int mat[2][3] = {{11,12,13}, {14,15,16}};

b) reading and writing

for (int r = 0; r < rows; r++)//that 's ok
{
    for(int c = 0; c < cols; c++)//column
        cout << mat[r][c] << ",";
    cout << endl;
}

c) Two dimensional array of unknown length

void init_2d_array(float mat[][], //error
              size_t rows, size_t cols)
void init_2d_array(float mat[][3], 
              size_t rows, size_t cols)

Be sure to write down how many columns there are, otherwise you can't find the second row, only the first address of the first row

Index

Value

Address

 

 

p+25

 

p+24

[1][2]

16

p+23

p+22

p+21

p+20

[1][1]

15

p+19

p+18

p+17

p+16

[1][0]

14

p+15

p+14

p+13

p+12

[0][2]

13

p+11

p+10

p+9

p+8

[0][1]

12

p+7

p+6

p+5

p+4

[0][0]

11

p+3

p+2

p+1

p+0

 

p-1

 

p-2

Memory is one-dimensional and can only be stored in one-dimensional way

Save the first line and save the second line

6. Constant array

const float PI = 3.1415926f;
PI += 1.f; // error
const float values[4] = {1.1f, 2.2f, 3.3f, 4.4f};
values[0] = 1.0f; // error

Element cannot be modified

It is often used in functions. The array transfer function may be modified

float array_sum(const float values[], size_t length)
{
    float sum = 0.0f;
    for (int i = 0; i < length; i++)
    {
        sum += values[i];
        //values[i] = 0; //error to prevent such misoperation
    }
    return sum;
}

2, String

1. Array string

char rabbit[16] = {'P', 'e', 't', 'e', 'r'};//The spare will be set to zero
char bad_pig[9] = {'P', 'e', 'p', 'p', 'a', ' ', 'P', 'i', 'g'}; //a bad one! // An array without an end is out of bounds
char good_pig[10] = {'P', 'e', 'p', 'p', 'a', ' ', 'P', 'i', 'g', '\0'};//Add '\ 0' to mark the end of the string ('\ 0' encoding value is 0, character 0 'encoding value is not 0)

2. String length

size_t strlen( const char *str )
char name[10] = {'Y', 'u', '\0', 'S', '.', '0'};
cout << strlen(name) << endl;//2 

3. Constant string

char name1[] = "Southern University of Science and Technology";//First count the number of characters, then add 1 to create an array, then put the string, and then add 0 to truncate
char name2[] = "Southern University of "    "Science and Technology";//Connect two strings
char name3[] = "ABCD"; //how many bytes for the array?

0 / / integer 0

name3+4

'D'

name3+3

'C'

name3+2

'B'

name3+1

'A'

name3+0

String length 4, array length 5

Larger string space

const wchar_t[] s5 = L"ABCD"; 
const char16_t[] s9 = u"ABCD"; //since C++11
const char32_t[] s6 = U"ABCD"; //since C++11

4. String operation and attribute acquisition

a)copy

char* strcpy( char* dest, const char* src );//If the capacity of dst is not enough, such as src100 elements and dest length is 10, 100 characters will still be copied, overflowing 90 (out of bounds)
char *strncpy(char *dest, const char *src, size_t count);//Count copies count at most. Generally, count can be set as the minimum value in dest src

b) String connection

char *strcat( char *dest, const char *src );

c) Compare

int strcmp( const char *lhs, const char *rhs );

It compares two strings according to the value of ACSII code; strcmp() function first subtracts the first character of s2 from the first character value of s1 string. If the difference is zero, continue the comparison; If the difference is not zero, the difference is returned

5.string class

std::string str1 = "Hello";
std::string str2 = "SUSTech";
std::string result = str1 + ", " + str2;//Add three strings

Length result length()

Wider characters

std::string 
std::wstring 
std::u8string //(C++20)
std::u16string //(C++11)
std::u32string //(C++11)

Keywords: C++ OpenCV

Added by angus930 on Wed, 02 Feb 2022 01:53:42 +0200