Basic grammar

The main function:

Each individual program must have a main function unless the program is a library.

int main(){ return 0; } int indicates that the return type is int, void indicates that no data is returned, void can be omittedCopy the code

iostream.h:

This is the basic I/O header file that we use as follows:

#include <iostream.h>

using namespace std;
Copy the code

Use. H is the old C++ or C style, in the new C++ style, generally not applicable.

#include <iostream>
Copy the code

Comment code:

// test.cpp
Copy the code

Output the log:

    int i = 0;
    
    std::cout << "test" << std::endl;
    std::cout << i << std::endl;
    
Copy the code

Get input:

std::cin.get(); Or: long distance; Cout << "input distance:" <<endl; cin >> distance;Copy the code

You can write it like this: c-in,c-out, for CPP input and output, printf is not recommended here, and why don’t I know

Namespace:

Did you find the input and output above troublesome? That’s right

using namespace std
Copy the code

The using directive assigns all names in the STD namespace to this class.

A namespace is a variety of visible scopes of indicators. Namespaces are defined with the namespace keyword. All identifiers in the C++ standard library are defined in a namespace named STD. A namespace is a C++ mechanism for grouping together a large number of logically related program entities under a single identifier. This identifier is used as the name of the group.

For example, print the complete statement as follows:

STD: : cout < < STD: : hex < < < < 3.4 STD: : endl;Copy the code

Using namespace STD:

using namespace std; int main() { cout << "hello world" << endl; // Cout << "hello world\n"; return 0; }Copy the code
  • All identifiers defined within the namespace STD are thus valid as if they were declared as global variables
  • A namespace is a way to encapsulate library names, like a wall around each library.
  • Using namespace STD is not recommended in header files, only in CPP.

Function variants:

As with overloading in Java, a function can set multiple different parameters

Use multiple functions:

When you customize a function, you need to define header methods. Using Namespace STD is placed externally so that all functions can access STD members.

#include <iostream> using namespace std; double getSum(double a, double b); Int main(){double result = getSum(1.0,2.0); cout<<result<<endl; return 0; } double getSum(double a, double b){ double c = a + b; return c; }Copy the code

Underlying data types

What are the underlying data types in CPP? Char short int Double Long bool float

There are also some combination types: long double long long long float long int short int

Special identifier: unsigned signed long short

For details, please refer to www.w3cschool.cn/cpp/cpp-dat…

Char contains only one byte. The value ranges from -128 to 127. An unsigned char stands for 0-255

Short is 2 bytes, int is 4 bytes, long is 8 bytes, float is 4 bytes and double is 8 bytes

What do signed and unsigned mean?

  • Signed exists by default, and values include negative values
  • Unsigned identifies an unsigned type, which simply does not include negative values

For example, int ranges from minus 2 to the 16th to plus 2 to the 16th

Unsigned int values range from 0 to 2 to the power of 32

Char is not qualified or unqualified by default and is determined by the C++ implementation.

What happens when you manipulate data beyond this value range?

The following defines the maximum and minimum values for different int types:

#include <iostream> using namespace STD; int main(){ int i = INT_MAX; int i_m = INT_MIN; cout << i_m << "~" <<i << endl; int i_8 = INT8_MAX; int i_8_m = INT8_MIN; cout << i_8_m << "~" << i_8 << endl; int i_16 = INT16_MAX; int i_16_m = INT16_MIN; cout << i_16_m << "~" << i_16 << endl; int i_32 = INT32_MAX; int i_32_m = INT32_MIN; cout << i_32_m << "~" << i_32 << endl; unsigned int i_64 = INT64_MAX; unsigned int i_64_m = INT64_MIN; cout << i_64_m << "~" << i_64 << endl; system("pause"); return 0; } Output: -2147483648 to 2147483647 -128 to 127-32768 to 32767 -2147483648 to 2147483647 0 to 4294967295Copy the code

Let’s test if the data type is out of range:

int i = INT_MAX; int i_m = INT_MIN; cout << i_m << "~" <<i << endl; The output minimum and maximum values are: -2147483648 to 2147483647 // the maximum value +1, the minimum value -1 i++; i_m--; cout << i_m << "~" <<i << endl; Output: 2147483647 ~ 2147483648Copy the code

You can see that the value range of the data is always a closed loop, and the same is true for unsigned types.

Const qualifier:

Kotlin generates compiler non-modifiable constants: const val TEST = 1

C++ also has a const qualifier.


#include <iostream>
using namespace std;

const int Month = 1;
const int arr[3] = {1,2,3};
#define NUM 5;

int main(){

    const int Weekend = 2;
    
    cout<<Month<<endl<<Weekend<<endl;

    for (size_t i = 0; i < 3; i++)
    {
        cout << arr[i] << endl;
    }
    

    system("pause");
    return 0;
}

Copy the code

Traditional C uses #define to define:

#define NUM 5;
Copy the code

But it is no longer recommended in C++

Floating point Numbers:

The E notation can be used in CPP to display floating point types, for example

float i = 12E3; float j = 12E-3; float k = -12E3; Float l = 3.14 e-10;Copy the code
  • I stands for 12 times 10 to the third
  • J is 12 divided by 10 to the third power
  • K is minus 12 times 10 to the third

Typedef statement:

Define aliases, similar to typeAlias type aliases in Kotlin, except that they are rarely used in Kotlin.

typedef int HAHHA;

int main(){

    HAHHA i = 123;
    cout<<i<<endl;
    
    system("pause");
    return 0;
}
Copy the code

Enumeration enum

enum Color {red,blue,green};


int main(){
    Color color = red;
    cout<<color<<endl;
        return 0;
}
Copy the code

The color output is 0 because the enumeration class defaults to 0 and increases in order. You can also customize the value.

String: We can use char[] to represent strings:

    char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
    char name[] = {"hahahahahah"};
    char tets[7];

    cout << site << endl;
    cout << name <<endl;

    strcpy(tets,site);
    cout << tets <<endl;
Copy the code

Use the string:

Add header file: #include <cstring> string title; Cout <<" type title:"<<endl; cin>>title; cout << title <<endl;Copy the code

String manipulation:

Char strcpy(tets,site); cout << tets <<endl; Int length = strlen(name); cout << length <<endl; int length2 = title.size(); cout << length2 <<endl; Cout << strcat(site,name) << endl; string id = "1234"; string result = name + id; cout << result <<endl;Copy the code

struct

Create a structure:

struct People
{
    int id;
    string name;
    float weight;
};
Copy the code

Create:

People people; people.id = 1; people.name = "xiaoming"; People. Weight = 130.0 f;Copy the code

C++ 11 can be abbreviated:

// c++11 People test {2,"hah",120.0f};Copy the code

You can create a default object:

struct People { int id; string name; float weight; } people {1,"xlu",140.0f};Copy the code

Structural array:

/ People/structure array peopleArr [2] = {{1, "xlu", 140.0 f}, {1, "xlu", 140.0 f}};Copy the code

The appropriate:

A common contains multiple type members, but only one can be used at a time

union ONE { /* data */ int i; float j; double ids[10]; }; int main(){ ONE one; one.i = 123; cout << one.i <<endl; One. J = 1.0 f; cout << one.i <<endl; //one.i will lose cout << one.j <<endl; system("pause"); return 0; }Copy the code

Use with structure:

struct people
{
    string name;
    union TEST
    {
        int id;
        float id_f;
    } test;
    
};
Copy the code

Can also be set to anonymous Commons:

struct PEOPLE2
{
    string name;
    union {
        int id;
        float id_f;
    };
};
Copy the code