#1. Constants are defined in two ways:

1.#define macro constant #define Day 7 2. Const int month=12; Keywords cannot be modified. Keywords are pre-reserved words (identifiers) in C++. Do not use keywords when defining variables or constants. Identifier (a self-assigned name for a variable or constant) naming rules: 1 The identifier cannot be a keyword. 2. The identifier contains only letters, digits, and underscores (_). The first character must be a letter or underscore (cannot start with a number!!). 4. Case sensitive letters in identifiers: It is best to name variables as they are known by name

#2. Data types

C++ specifies that when creating a variable or constant, the corresponding data type must be specified, otherwise the variable cannot be allocated memory. Data types exist for a reason: to allocate appropriate memory space to variables. For example, int 10 need not be as long as double to avoid wasting space. Integer variables represent data of integer type. There are several ways to represent integer types in C++, depending on how much memory they take up. Data Type Space Occupied Value Range Short (short integer) 2 bytes (-2^15 to 2^15-1) int(integer) 4 bytes (-2^31 to 2^31-1) LONG (long integer) 4 bytes (32-bit) in Windows and Linux. 8 bytes (64 bits) (-2^31 to 2^31-1) Long Long (long integer) 8 bytes (-2^63 to 2^63-1) The memory usage varies depending on the number of bytes. So the range of values is different, and if you go beyond the range, you get an error. The sizeof keyword is used to measure the sizeof memory occupied by data types. Syntax sizeof(data type/variable) #include< iostream> using namespace STD; Int main() {// Short (2) int (4) long (4) long long (8) << sizeof(short) << endl; << sizeof(num1) << endl;

system("pause");
return 0;
Copy the code

}

##2.3 Real (floating point) is used to represent decimals. Floating point variables fall into two categories:

  1. Single-precision float 4 bytes 7 significant digits
  2. Double 8 bytes 15 to 16 digits Digits: Number of digits before and after the decimal point Example: 3.14 Contains 3 digits ##2.4 Character function: Character variable displays a single character. Syntax: char ch= ‘a’; Note 1: Use single quotes, not double quotes; Note 2: A single quotation mark contains only one character, not a string. • Character variables in C and C++ take up only one byte • Character variables do not store the characters themselves in memory, Cout <<(int)ch<<=”” code=”” style=”box-sizing: border-box; > ##2.5 Escape characters: used to represent some ASCII characters that cannot be displayed. Commonly used: \n newline, \ output a \, \t TAB ##2.6 String: used to represent a string of characters. 2.C++ style string: string name =” string value “must include header file #include ##2.7 Boolean bool Function: Boolean data types represent true or false values. Bool has only two values:
  • True – true (essence 1)
  • False — false (essentially 0) bool 1 byte size ##2.8 Data input: used to fetch data from keyboard Operator function: An operation used to execute code. ##3.1 Arithmetic operators
  • +, -, *, /
  • % mod (mod) is essentially finding the remainder. You cannot modulo 0 or two decimals (C++)
  • Increment decrement ++, — increment before, first let the variable + 1, then the expression operation; Postincrementing, the expression is evaluated first, then the variable + 1; 前置 : int a2 = 10; int b2 = ++a2 * 10; cout << “a2=” << a2 << endl; cout << “b2=” << b2 << endl;

Int a3 = 10; a2=11,b2=110; int b3 = a3 ++* 10; cout << “a3=” << a3 << endl; cout << “b3=” << b3 << endl;

The final output result is as follows: A3 =11, B3 =100. The assignment operator assigns the value of an expression to a variable. =, +=, -=, *=, /=, %= a+=2 is the same as a=a+2, and the rest is the same. == == =! The Boolean operator returns a true or false value for a value used in an expression. Operator term example result! Non! A If a is false then! A is true; If a is true, then! A false && with a && b if a and b are true, the result is true, otherwise false | | or a | | b, if a and b have a is true, the result is true, when both are false, the result is false

C/C++ supports three basic execution structures: sequential structure, selection structure, and loop structure. Use the ternary operator to make simple judgments. Syntax: Expression 1? Expression 2: Expression 3 Explanation: If the value of expression 1 is true, expression 2 is executed and the result of expression 2 is returned. If the value of expression 1 is false, expression 3 is executed and the result of expression 3 is returned. In C++ ternary operators return variables and can continue to assign. Execute a multi-condition branch statement. Syntax: switch (expression) {

Case result 1: Execute the statement. Break; Case result 2: Execute the statement. Break; . Default: execute the statement. Break;Copy the code

}

You must use break to exit the current branch; otherwise, the execution will continue down. Difference between if and switch: -switch Disadvantages: The value can only be an integer or character, not a range (the content after switch ()) – Switch Advantages: clear structure and high execution efficiency. If a loop condition is met, execute a loop statement. A loop statement is executed as long as the result of a loop condition is true. Note: When executing a loop statement, the program must provide an exit from the loop, otherwise an infinite loop will occur. Generate random number: Rand ()%100 generate a random number from 0 to 99 (pseudo-random number) #include< ctime> srand ((unsigned int)time(NULL));

# # # out the do… A while statement executes a loop if a condition is met. Syntax: do{loop}while (loop condition) The while executes a loop statement and then evaluates the loop condition. Daffodil number Daffodil number daffodil number is a 3-digit number in which the sum of the three powers of each digit is equal to itself. For example, 1^3+5^3+3^3=153 While statement to find the number of daffodils in all three digits. #include< iostream> using namespace std;

Int main() {//1. Print all three digits int num = 100; Int a = 0; do {//2. // int b = 0; Int c = 0; // a = num % 10; // get bits b = num / 10%10; C = num / 100; // Get the hundreds digit

If (a * a * b + a + b * b * * * c = c = c num) / / if it is a daffodil number, only print {cout < < num < < endl; } num++; } while (num < 1000); system("pause"); return 0;Copy the code

}

Execute a loop statement if a loop condition is met. Syntax: for(start expression; Conditional expression; End loop body){loop statement; Case description: Count from 1 to 100, if the ones digit contains 7, or the tens digit contains 7, or if the number is a multiple of 7, we print the table, and print the rest of the numbers directly. #include< iostream> using namespace std;

Int main() {//1. For (int I = 1; i <= 100; I++) {/ / 2. Found in the 100 digital special digital, print “tapping” / / if it is a multiple of 7, there are 7, there are 7 or 10 bits hitting table to print the if (I % 7 = = 0 | | I % 10 = = 7 | | I / 10 = = 7) / / if it is a special number, Print knock table {cout <<” knock table “<< endl;

} else// Print numbers only if not special numbers {cout << I << endl; } } system("pause"); return 0;Copy the code

}

#include< iostream> using namespace STD;

Int main() {for (int I =1; i <= 9; i++) { //cout << i << endl; for (int j = 1; j <= i; j++) { cout << j << “” << i << “=” << i * j << ” “<<“\t”; //\t horizontal TAB} cout << endl; }

system("pause");
 return 0;
Copy the code

}

The break statement is used to break the selection structure or loop structure. Break When to use:

  • Occurs in switch condition statements to terminate the case and exit switch*
  • Appears in a loop to break out of the current loop
  • ###4.3.2 Continue In a loop, the next loop is executed by skipping the remaining statements in the current loop. #include< iostream> using namespace std;

Int main() {//continue statement for (int I = 0; i <= 100; I++) {// if the output is odd, the output is not even. if (i % 2 == 0) { continue; } cout << i << endl; } system(“pause”); return 0; }

Break exits the loop directly, while continue does not. The goto statement is used to jump statements unconditionally. Syntax: goto tag; Explanation: If the tag name exists, the goTO statement jumps to the tag’s location. Not recommended to avoid process confusion. An array is a collection of data elements of the same type. Attribute 1: Each data element in an array is of the same data type. Attribute 2: Arrays are made up of contiguous memory locations. One-dimensional arrays are defined in a way 1. Data type Array name [array length] 2. Array name [array length]={value 1, value 2….. }; // If the data is not completely filled in during initialization, 0 will be used to fill in the remaining data. 3. Data type Array name []={Value 1. Value 2…… }; // When you define an array, you must have an initial length (i.e. at least one data element in {}). The use of one-dimensional array names is as follows:

  1. You can count the length of the entire array in memory.
  2. You can get the first address of the array in memory. #include< iostream> using namespace std; Int main() {// Array usage //1. Count the length of the entire array in memory. Int arr[10] = {1,2,3,4,5,6,7,8,9,10}; << sizeof(arr) << endl; << sizeof(arr[0]) << endl; The number of elements in the array is: “<< sizeof(arr)/sizeof(arr[0]) << endl; //2. Get the first address of the array in memory. << arr << endl; // hexadecimal cout << “the first address is:” << (int)arr << endl; Cout << “The address of the first element in the array is:” << (int)&arr[0] << endl; The address of the second element in the array is: “<< (int)&arr[1] << endl; // Next to the first one, just four bytes away. // Array names are constants and cannot be assigned. //arr = 100; Error system (” pause “); return 0;

} Array names are constants and cannot be assigned. #include< iostream> using namespace STD;

Int arr[5] = {1,3,2,5,4}; “<< endl; for (int i = 0; i < 5; i++) cout << arr[i] << endl; 2.1 Record the position of the initial subscript //2.2 record the position of the end subscript //2.3 Interchange of the elements of the initial subscript and the end subscript //2.4 Start ++, end position — //2.5 Perform the operation 2.1 until start >= end position int start = 0; Int end = sizeof(arr)/sizeof(arr[0]) -1; While (start < end) // Important: stop condition, I didn’t think of it before. {// Switch code int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // subscript update start++; end–; } //3. Print the array inverted cout << “array elements inverted:” << endl; for (int i = 0; i < 5; i++) cout << arr[i] << endl;

system("pause");
 return 0;
Copy the code

} Stop condition note ##5.2.3 Bubble sort: the most common sort method is to sort the elements of an array.

  1. Compare adjacent elements, and if the first is larger than the second, swap them both.
  2. Do the same for each pair of adjacent elements, and when you’re done, find the first maximum.
  3. Repeat the above steps with a count of -1 until no comparison is required. #include< iostream> using namespace STD;

Int main() {// Use bubble sort to implement ascending sequence. Int arr[9] = {4,2,8,0,5,7,1,3,9}; << endl; for (int i = 0; i < 9; i++) { cout << arr[i] << ” “; } cout << endl; For (int I = 0; i < 9 – 1; For (int j = 0; int j = 0; int j = 0; j < 9 – i – 1; If (arr[j] > arr[j + 1]) {int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; Cout << “cout:” << endl; for (int i = 0; i < 9; i++) { cout << arr[i] << ” “;

}
cout << endl;

system("pause");
 return 0;
Copy the code

} Note that the total number of sorting rounds = number of elements – number of current rounds -1, and the number of inner loops = number of elements – number of current rounds -1 There are four ways to define a two-dimensional array:

  1. Data type Array name [number of rows][number of columns]

  2. Data type Array name [row number][column number]={{data 1, data 2}, {data 3, data 4}}

  3. Array name [number of rows][number of columns]={data 1, data 2, data 3, data 4}

  4. Data type array name [][number of columns]={data 1, data 2, data 3, data 4} – Check the memory size of a 2-bit array. – Gets the first address of a two-dimensional array. #include< iostream> using namespace STD; Int main () {/ / two-dimensional array an array of int arr [2] [3] = {{1, 2, 3}, {4 and 6}}; Cout << “array size:” << sizeof(arr) << endl; << sizeof(arr[0]) << endl; Cout < < “two-dimensional array element size:” < < sizeof (arr [0] [0]) < < endl;

    << sizeof(arr)/sizeof(arr[0]) << endl; Cout < < “two-dimensional array columns:” < < sizeof (arr [0])/sizeof (arr [0] [0]) < < endl; // address cout << “first address of two-dimensional array” << (int) arr << endl; Cout << “address of the first row of two-dimensional array” << arr[0] << endl; Cout << “address of the second row of the two-dimensional array” << arr[1] << endl;

    Cout < < “two-dimensional array first element address” < < (int) & arr [0] [0] < < endl; Cout << “address of the second element of the two-dimensional array” << &arr[0][1] << endl;

    system(“pause”); return 0;

A large program, usually divided into blocks, each of which performs a specific function. The definition of a function generally consists of 5 steps:

  1. Return value type
  2. The function name
  3. The list of parameters
  4. Function body statement
  5. Return expression syntax: Return value type Function name (argument list) {function body statement return expression} // function definition // syntax: Int add(int num1, int num2) {int sum = num1 + num2; int sum = num1 + num2; return sum; } ##6.3 Function call function: use a defined function. #include< iostream> using namespace STD;

Int add(int num1, int num2) {int sum = num1 + num2; return sum; }

Int main() {// add a = 10; int b = 20; // When a function is called, the value of the argument is passed to the parameter int c = add(a, b); cout << “c= ” << c << endl;

system("pause");
return 0;
Copy the code

#include< iostream> using namespace STD; #include< iostream> using namespace STD; Void void swap(int num1, int num2) {cout << “before exchange:” << endl; cout << “num1= ” << num1 << endl; cout << “num2= ” << num2 << endl;

int temp = num1; num1 = num2; num2 = temp; Cout << "after exchange:" << endl; cout << "num1= " << num1 << endl; cout << "num2= " << num2 << endl; //return; You can omit reurn if the return value is not neededCopy the code

} int main() { int a = 10, b = 20; cout << “a= ” << a << endl; cout << “b= ” << b << endl; Swap (a, b); swap(a, b); cout << “a= ” << a << endl; cout << “b= ” << b << endl; //a and B do not change system(“pause”); return 0; } ##6.5 Common forms of functions There are four common forms of functions:

  1. Without reference to return
  2. With and without return
  3. No arguments have to return
  4. Function declaration: tells the compiler the name of the function and how to call it. The actual body of a function can be defined individually. The declaration of a function can be multiple times, but the definition of a function can only be defined once.

// A comparison function that compares two integer numbers and returns the larger value

Int Max (int a, int b); int Max (int a, int b); int max(int a, int b); int max(int a, int b); int max(int a, int b); int main() { int a = 10; int b = 20; cout << max(a, b) << endl;

system("pause");

return 0;
Copy the code

}

Int Max (int a, int b) {return a > b? a : b; } ##6.7 Function split file writing function: make code structure clearer. There are generally four steps in writing a function split file:

  1. Create a header file with the suffix.h
  2. Create a source file with the suffix.cpp.
  3. Write the declaration of the function in the header file.
  4. Write the function definition in the source file. H #include< iostream> using namespace STD; Void swap(int a, int b); CPP #include”swap.h”

Void swap(int a, int b) {int temp = a; a = b; b = temp; cout << “a= ” << a << endl; cout << “b= ” << b << endl; } program #include using namespace STD; #include”swap.h”

//1. Create a header file with suffix. H //2. Create a source file with suffix. //3. Write the function declaration in the header file. //4. Write the function definition in the source file. int main() { int a = 10; int b = 20; swap(a, b);

system("pause");
return 0;
Copy the code

}

Pointers are used to access memory indirectly. – Memory numbers start from 0 and are usually expressed as hexadecimal numbers. – You can use pointer variables to store addresses. Int a=10; int a=10; int p; p=&a; // the pointer p is the address of a, & is the addresser, &a and p are the same output. P =1000; p=1000; p=1000; p=1000; p=1000; // Change p is the same as change a, now a=1000

Q: Pointers are also a data type. How much memory does this data type occupy? A: 32-bit operating systems are four bytes regardless of the data type, and 64-bit is eight bytes. Development is typically done on 32-bit systems. Null pointer: Pointer variable refers to a space in memory numbered 0. Use: Initialize pointer variable Note: memory to which a null pointer points is not accessible. #include< iostream> using namespace std; Int main() {// a NULL pointer is used to initialize a pointer variable int *p = NULL;

*p = 0; *p = 0; *p = 0; System ("pause"); return 0;Copy the code

}

Wild pointer: Pointer variable points to an invalid memory space. #include< iostream> using namespace std;

Int main() {int *p = (int *)0x1100; Cout << *p << endl; // Once manipulated, an error will be reported

system("pause");

return 0;
Copy the code

}

Neither the null pointer nor the wild pointer is our own requested space, so do not access it. Const const decorates a pointer

  1. Const decorates a pointer – a constant pointer

  2. Const modifies constants – pointer constants

  3. Const const int a = 10; int b = 10; int *p = &a; Const int *p = &a; //1. // The pointer pointer can be changed, but the pointer value cannot be changed. *p = 20; // error, pointer to the value can not be changed; // The pointer pointer can be changed

    //2. Const const const int * const p = &a; *p = 20; *p = 20; P = &b; // Error, pointer pointer cannot be changed

    //3. Const int *const p = &a; *p = 20; // error p = &b; // Const const const const const const const const const const const const const const const const const

Use Pointers to access elements in an array. #include< iostream> using namespace std;

Int arr[10] = {1,2,3,4,5,6,7,8,9,10};

The first element is "<< arr[0] << endl; int *p = arr; //arr is the first address of the array cout << "Use a pointer to access the first element:" << *p << endl; p++; Cout << "Use the pointer to access the second element:" << *p << endl; Cout << "traverse the array with a pointer" << endl; int *p2 = arr; for (int i = 0; i < 10; i++) { //cout << arr[i] << endl; cout << *p2 << endl; p2++; } system("pause"); return 0;Copy the code

}

#include< iostream> using namespace STD; Void swap01(int a, int b) {int temp = a; a = b; b = temp; cout << “swao01 a= ” << a << endl; cout << “swap01 b= ” << b << endl; } void swap02(intp1, intp2) { int temp = *p1; *p1 = *p2; *p2 = temp; } int main() {// main() {//1. int b = 20; //swap01(a, b);

Swap02 (&a, &b); swap02(&a, &b); swap02(&a, &b); cout << "a= " << a << endl; cout << "b= " << b << endl; system("pause"); return 0;Copy the code

} Summary: if you do not want to change the argument, pass it by value, if you want to change the argument, pass it by address. Case description: encapsulate a function, using bubble sort, the realization of integer array ascending sort. For example, int arr[10]={4,3,6,9,1,2,10,8,7,5}; #include< iostream> using namespace std; Void bubbleSort(int *arr,int len) {for (int I = 0; i < len – 1; i++) { for (int j = 0; j < len – i – 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; }}}} // Print the array void printArray(int *arr, int len) {for (int I = 0; i < len; i++) { cout << arr[i] << endl; }} int main () {/ / 1. To create an array int arr [10] =,3,6,9,1,2,10,8,7,5 {4};

Int len = sizeof(arr)/sizeof(arr[0]); //2. Create a function to implement bubbleSort(arr, len); //3. Print sorted array printArray(arr, len); system("pause"); return 0;Copy the code

}

Struct is a user-defined data type that allows users to store different data types. There are three ways to create a variable from a struct: – struct name variable name – struct name variable name ={member 1 value, member 2 value… . #include< iostream> using namespace STD; #include

Struct type name {member list} struct Student {// Member list // name string name; // int age; // score int score; }s3; // Create struct variables

//2. Create a specific student by student typeCopy the code

int main() {

Struct Student s1; //struct Student s1; //Student s1; // Assign the s1 attribute to pass. Access the struct variable attribute s1.name = "zhang SAN "; s1.age = 18; s1.score = 100; Cout < < "name:" < < s1. The name < < "age:" < < s1. Age < < "score:" < < s1. Score < < endl; / / 2.2 struct Student s2 = {... Struct Student s2 = {struct Student s2 = {struct Student s2 = { Cout < < "name:" < < s2. Name < < "age:" < < s2. Age < < < < "score:" s2. Score < < endl; // create struct s3.name = "king5 "; s3.age = 20; s3.score = 60; Cout < < "name:" < < s3. The name < < "age:" < < s3. Age < < < < "score:" s3. Score < < endl; system("pause"); return 0;Copy the code

}

Struct; struct; struct; struct; struct; Struct struct name Array name [element number]={{},{},…. {}}; #include< iostream> using namespace STD; #include

Struct Student {// name string name; // int age; // score int score;

}; Int main () {/ / 2. Create an array of structure struct Student stuArray [3] = {{” zhang “, 18100}, {” bill “, 28 13}, {” detective “, 38,66}

}; StuArray [2]. Name = "zhao 6 "; stuArray[2].age = 86; stuArray[2].score = 60; For (int I = 0; i < 3; I++) {cout < < "name:" < < stuArray [I] name < < "age:" < < stuArray [I] the age < < "score:" < < stuArray [I] score < < endl; } system("pause"); return 0;Copy the code

}

#include< iostream> using namespace STD; #include< iostream> #include

Struct Student {string name; // name int age; // age int score; / / score};

Int main() {struct Student s = {“张三”,18,100}; / / struct can be omitted

Student *p = &s; << p->name << "age:" << p->age << "分 :  " << p->score << endl; system("pause"); return 0;Copy the code

}

Summary: A structure pointer can access a member in a structure by using the -> operator. #include< iostream> using namespace STD; #include< iostream> #include

Struct student {string name; int age; int score; };

Struct teacher {int id; // Teacher id string name; // teacher name int age; Struct student stu; // Tutor student};

Int main() {// create teacher t; t.id = 100000; T. name = “Lao Wang “; t.age = 50; T.stu.name = “xiaowang “; t.stu.age = 20; t.stu.score = 60; Cout << “Teacher name:” << T.name << “Teacher number” << T.ID << “Teacher age:” << T.age << name of student tutor: “<< T.stu.name <<” Student age: “< < t.s tu. Age < <” students score: “< < t.s. Tu score < < endl;

system("pause");
return 0;
Copy the code

}

Summary: Within a structure you can define another structure as a member to solve a real problem. Structs are passed to functions as arguments. Example: #include< iostream> using namespace STD; #include

Struct student {string name; int age; int score; }; Void printStudent1(struct student s) {s.age = 100; Cout << “subfunction print result name:” << s.name << “age:” << s.age << “score:” << s.core << endl; }

Void printStudent2(struct student *p) {p->age = 101; Cout < < “sub function 2 results of print name:” < < p – > name < < “age:” < < p – > age < < “score:” < < p – > score < < endl; }

Int main() {// Pass the struct as a function argument // Pass the student as an argument, print all the information about the student

Struct student s; S.name = ""; s.age = 20; s.score = 85; printStudent1(s); printStudent2(&s); Cout << "main print result name:" << s.name << "age:" << s.age << "score:" << s.core << endl; system("pause"); return 0;Copy the code

}

If you do not want to modify the data in the main function, pass it by value; otherwise, pass it by address. Use const to prevent misoperation. Example: #include< iostream> using namespace STD; #include

Struct student {string name; int age; int score; }; Void printStudents(const student *s) {//s->age=150; Cout << “name:” << s->name << “age:” << s->age << “score:” << s->score << endl; }

Int main() {struct student s = {” 三”,15,70};

PrintStudents (&s); printStudents(&s); system("pause"); return 0;Copy the code

}

Case Description: The school is doing a graduation project, each teacher leads 5 students, there are three teachers in total, the need is to design the structure of students and teachers as follows, in the teacher structure, there are teacher name and an array to store 5 students as members. #include< iostream> using namespace STD; #include< iostream> using namespace STD; #include #include

Struct Student {// name string sName; // score int score;

}; Struct Teacher {// name string tName; Struct Student sArray[5]; struct Student sArray[5];

}; Void allocateSpace(struct Teacher tArray[],int len) {string nameSeed = “ABCDE”; For (int I = 0; i < len; i++) { tArray[i].tName = “Teacher_”; tArray[i].tName += nameSeed[i];

// assign for (int j = 0; j < 5; j++) { tArray[i].sArray[j].sName = "Student_"; tArray[i].sArray[j].sName += nameSeed[j]; int random = rand() % 61 + 40; //40~100 tArray[i].sArray[j].score = random; }}Copy the code

Void printInfo(struct Teacher tArray[],int len) {for (int I = 0; i < len; I++) {cout < < “the teacher’s name:” < < tArray [I] tName < < endl;

for (int j = 0; j < 5; << tArray[I].sarray [J]. SName: "<< tArray[I].sarray [J]. }}Copy the code

} int main() {// Srand ((unsigned int)time(NULL)); Struct Teacher tArray[3];

Int len = sizeof(tArray)/sizeof(tArray[0]); allocateSpace(tArray,len); PrintInfo (tArray, len); system("pause"); return 0;Copy the code

}

Case Description: Design a hero structure, including members name, age, gender; Create an array of structures that hold 5 heroes. Through the method of bubble sort, the heroes in the array are sorted in ascending order according to the age, and finally print the sorted result. The five heroes are as follows: 1 23 4 5 6 {” Liu Bei “, 23, “male “}; {” Guan Yu “, 22, “Male “}; {” zhang Fei “, 20, “male “}; {” zhao Yun “, 21, “male “}; {” Diao Chan “, 19, “female “};

Example: #include< iostream> using namespace STD; #include

Struct Hero {// name string name; // int age; // Gender string sex;

};

Void bubbleSort(struct Hero heroArray[], int len) {for (int I = 0; i < len – 1; i++) { for (int j = 0; j < len – i – 1; If (heroArray[j]. Age > heroArray[j +1]. Age) {struct Hero temp = heroArray[j]; HeroArray [j] = heroArray[j + 1]; heroArray[j] = heroArray[j + 1]; heroArray[j + 1] = temp; }}

}
Copy the code

Void printHero(struct Hero heroArray[], int len) {for (int I = 0; i < len; I ++) {cout << “name:” << “age:” << “age:” << “heroArray[I]. }} int main () {/ / 2. Create an array to store five heroes struct Hero heroArray [5] = {{” liu2 bei4 “, 23, “male”}, {” guan yu “, 22, “male”}, {” zhang fei “, 20, “male”}, {” zhaoyun,” 21, “male “}, {” Diao Chan “, 19,” female “}};

int len = sizeof(heroArray) / sizeof(heroArray[0]); BubbleSort (heroArray, len); bubbleSort(heroArray, len); //4. Print printHero(heroArray, len); system("pause"); return 0;Copy the code

}

For Word documents, please email [email protected]