虚幻Unreal - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(37评价)
价格: 1639.00元

cout  << "hello"//输出

endl //换行 无法放在字符串中

\n //换行 可以放在字符串中

[展开全文]
东北军阀张美玉 · 2021-10-18 · 0

选中后先按ctrl+K 再按ctrl+C 是注释 

按住ctrl+K 再按ctrl+U是取消注释

[展开全文]
东北军阀张美玉 · 2021-09-26 · 0

练习题

    1.int i;

       for(i = 0; i < 5; i++)

           cout << i;

           cout << endl;

    这段代码将输出:

    01234

 

    2.int j;

       for(j = 0; j < 11; j += 3)

           cout << j;

       cout << endl << j << endl;

    这段代码将输出:

    0369

   

    12

 

    3.int j = 5;

       while(++j < 9)

           cout << j++ << endl;

    这段代码将输出:

    6

    8

    4.int k = 8;

       do

           cout << "k = " << k << endl;

       while(k++ < 5);

    这段代码将输出:

    k = 8;

    

    5.

#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;

int main()
{
    int result = 1;
    for (int i = 0; i < 7; i++)
    {
        cout << result << " ";
        result *= 2;
    }
}

       6.

#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;

int main()
{
    int a = 0;
    int b = 0;
    int total = 0;
    cout << "Enter a number:";
    cin >> a;
    cout << "Enter another number that larger than first one:";
    cin >> b;
    for (; a <= b; a++) {
        total += a;
    }
    cout << "The sum of integers between two numbers is:" << total << endl;
}

    7.

#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;

int main()
{
    int temp = 0;
    int sum = 0;
    cout << "Enter a number(Enter zero mean quit program):";
    cin >> temp;
    while (temp != 0) {
        sum += temp;
        cout << "Current sum is:" << sum << endl;
        cout << "Enter a number(Enter zero mean quit program):";
        cin >> temp;
    }
}

 

[展开全文]
YH1209 · 2020-08-21 · 0

多维数组

    声明多维数组的基本结构如下:

    int testArray[2][2]{

        {11,22},

        {33,44}

    };

    std::array<array<int, 2>,2> testArray2;

    testArray2[0] = {{11,22}};

    testArray2[1] = {{33,44}};

    上述代码声明了一个2维数组testArray和testArray2,可以将第一个"[]"内的数字看做行数,第二个"[]"内的数字看做列数。

    多维数组的声明和遍历操作如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;

int main()
{
    array<int, 10> testArray{11,22,33,44,55,66,77,88,99,00};
    array<int, 10> testArray2{22,33,44,55,66,77,88,99,00,11};
    array<int, 10> testArray3{33,44,55,66,77,88,99,00,11,22};
    array<array<int, 10>, 3> testArrayTotal;
    testArrayTotal = { testArray, testArray2, testArray3 };
    for (int i = 0; i < testArrayTotal.size();i++){
        for (int j = 0; j < 10; j++){
            cout << testArrayTotal[i][j] << "\n";
        }
        cout << "," << endl;
    }

}

 

[展开全文]
YH1209 · 2020-08-20 · 0

do-while循环

    do-while循环与while循环类似,区别在于do-while的函数体在条件判断语句的前面,所以无论条件满足与否,都会执行一次循环体。基本结构如下:

    do{

        loop body;

    }while(condition);

    其中,loop body表示循环体,condition表示条件。

    使用do-while遍历数组的操作如下:

array<int, 10> testArray;
int i = 0;
do{
    cout << testArray[i];
    i++;
}while(i < testArray.size());

遍历数组的新方式

    遍历数组的新方式操作如下:

array<int, 10> testArray;
for(int temp : testArray){
    cout << temp << endl;
}

    此方式只能够进行遍历操作,如果要使用此操作进行赋值,操作如下:

array<int, 10> testArray;
for(int& temp : testArray){
    temp = 10;
}

 

[展开全文]
YH1209 · 2020-08-20 · 0

While循环

    while循环是循环结构的一种,可以通过while()括号中设定条件,该循环会在满足设定的条件时重复执行循环体内的函数。while循环的基本结构如下:

    while(condition){

        loop body;

    }

    其中condition表示条件,loop body表示循环体。

    遍历一个数组的操作如下:

array<int,10> testArray;
int i = 0;
while(i < testArray.size()){
    std::cout << testArray[i] << endl;
    i++;
}

 

[展开全文]
YH1209 · 2020-08-20 · 0

    定义宏的操作如下:

    #define STR return 0;

    可以指定一个字符串来代替另外的一些字符串。如:

    #define END return 0;

    #define FSTRING string

    当定义了宏时,能够在使用被替换的字符串时键入宏名称来代替被替换的字符串。

    在为一个数据类型定义一个宏或函数时,不能使用";"符号结束。

类型别名

    用宏来实现类型别名外能够使用typedef来实现,操作如下:

    typedef int UInt

    需要使用";"符号结束。

    typedef只能为数据类型定义别名。

[展开全文]
YH1209 · 2020-08-20 · 0

for循环

    循环结构的一种,根据条件重复执行同一段代码,基本结构如下:

    for(init; condition; update){

        loob body;

    }

    其中,init部分为初始化一个用来判断条件的整型变量;condition部分为执行loob body的条件;update部分为更新用来判断条件的整型变量;loob body部分为循环体,即重复执行的代码。

    初始化部分只会执行一次,然后执行判断部分,如果判断结果为true,则执行一次循环体内的代码;如果判断结果为false,则结束循环。当执行了一次循环体内的代码后,会进入更新部分,更新完成后再进行判断。这个过程将重复执行至判断结果为false才会结束循环。

    如果要使用for循环遍历一个数组的所有元素,代码如下:

array<int, 10> testArray;
for(int i = 0; i < testArray.size(); i++){
    std::cout << testArray[i];
}

    

[展开全文]
YH1209 · 2020-08-19 · 0

第二次编程练习

#include <iostream>
#include <array>
#include <string>
using namespace std;

struct CandyBar {
	string brand;
	float weight;
	int cal;
};

int main()
{
	//1.
	array<char, 30> actor1;
	char actor2[30]{};
	array<float, 13> chuck1;
	float chuck2[30]{};

	//2.
	CandyBar snack{"Mocha Munch", 2.3f, 500};
	cout << "Candybar snack's info:\n" << "Name:" << snack.brand 
		<< "\nWeight:" << snack.weight 
		<< "g\nKcal:" << snack.cal << "cal." << endl;

	//3.
	array<float, 3> score;
	cout << "Enter three 50-meter sprint results:" << endl;
	cin >> score[0];
	cout << "Entered." << endl;
	cin >> score[1];
	cout << "Entered." << endl;
	cin >> score[2];
	cout << "Number of attempts is " << score.size() 
		<< ", average score is " 
		<< (score[0] + score[1] + score[2]) / 3
		<< "." << endl;
}

 

[展开全文]
YH1209 · 2020-08-19 · 0

模板类array

    C++11的标准将使用模板类创建数组,需要引入模板类array和命名空间std,如下:

    #include <array>

    using namespace std;

    int main(){

        array<type,elementCount> arrayName = {11,22,33,44};

        //type为数据类型,elementCount为元素个数,arrayName为数组名。

    }

    此方式可以对数组进行赋值操作,将能够将一个数组的元素复制给另一个数组。

    

[展开全文]
YH1209 · 2020-08-19 · 0

使用new关键字创建数组

    使用new关键字创建数组的方式如下:

    int* p = new int[elementCount];

    整型指针p将指向数组的第一个元素的地址。

    可以通过指针名来写入或读取数组的元素,如:

    p[0] = 7;

    cout << p[0] << endl;

    new创建的数组需要使用delete去释放内存,由于数组的连续的若干个地址组成的,所以释放内存操作如下:

    delete[] p;

[展开全文]
YH1209 · 2020-08-19 · 0

指针和数组

    C++中,输出数组会输出一个内存地址,表明数组也是指针的一种,它会在内存中申请连续的空间。数组名指向数组第一个元素的内存地址,对它进行取值操作会取到第一个元素的值,对数组名进行加减法时,会移动指针指向的内存地址,加法向后,减法向前。可以通过这种方式对指定下标元素进行赋值操作,如下:

    int test[] {11,22,33,44,55};

    *(test + 1) = 1000;

    上述操作会对数组test的第二个元素赋值1000。

[展开全文]
YH1209 · 2020-08-17 · 0

new关键字和delete关键字

    new关键字用法如下:

    int* p = new int;

    *p = 0;

    通过new关键字申请内存地址,一定要使用delete关键字释放内存,及时清空内存空间,delete关键字用法如下:
    delete p;

    声明的变量无需手动释放内存。

[展开全文]
YH1209 · 2020-08-17 · 0

空指针、空类型指针

    当无法知晓先前声明的指针具体指向哪一内存地址时,可以使指针指向空地址,如下:

    int* p1 = NULL;

    int* p2 = 0;

    C++11标准引入了新的空指针指定方式,如下:

    int* p3 = nullptr;

    可以指定空类型指针,如下:

    void* p4;

    表示p4可以接受任意类型的地址,有如下操作:

    int a = 0;

    int* pa = &a;

    p4 = &a;

    如果要使用空类型指针指向地址的值,需要将空类型指针进行强制类型转换,如:

    int b = *((int*)p4);

[展开全文]
YH1209 · 2020-08-17 · 0

指针

    1.指针可以用来存储变量的地址,声明指针如下:

    int a = 0;

    int* pa = &a;

    指针类型需要与变量的类型保持一致,否则将报错。

    2.直接输出指针会输出内存地址,所以要取得指针所指的内存地址内存储的值需要在指针前加入*符号,如:

    int a = 0;

    int* pa = &a;

    cout << *pa << endl;

    3.指针间的赋值操作如下:

    int a = 0;

    int* pa = &a;

    int* p;

    p = pa;

    4.通过指针去存储数据的操作如下:

    *pa = 100;

    此操作将会将pa指针指向的内存地址中存储的值设置为100,所以此前定义的变量a也会变成100。如果通过p指针去修改值,也会有同样的结果。

    *指针必须初始化,使用没有初始化的指针会报错。

[展开全文]
YH1209 · 2020-08-17 · 0

内存和地址

    内存是程序运行时,存储变量或常量的地方,可以把内存想象成一个字节数组,这个字节数组的元素下标就相当于内存的地址。

    内存中的每个位置都由一个独一无二的地址表示,内存中的每个位置都包含一个值,可以通过一个地址找到内存的具体位置后得到该地址中存储的值。

    在声明一个变量时,系统会自动为该变量申请内存空间,后续通过变量名去访问内存并取得该值,无需关心内存的地址。

    可以使用&符号取得一个变量的值,如:

    int i = 0;

    cout << &i << endl;

    内存的地址用16进制数字表示。

    可以使用*符号取得一个内存地址中存储的值,如:

    int i = 0;

    cout << *(&i) << endl;

    

[展开全文]
YH1209 · 2020-08-17 · 0

枚举类型

    枚举类型的声明如下:

    enum TestEnum{

        Type1,

        Type2,

        Type3

    };

    枚举类型的实际值是整型数据,可以当做整型去使用,枚举的第一个值为0。当使用cout去输出一个枚举类型的变量时,会输出整型。

    枚举的值是可以更改的,如:

    enum TestType{

        Type1 = 1;

        Type2 = 4;

        Type3 = 8;

    };

    在后续使用时需要保证指定的值存在于枚举中。

[展开全文]
YH1209 · 2020-08-17 · 0

结构体

    结构体类似于类,可以存储多类型的变量。它有自己的作用域,如果他声明在函数体外,则可以被多个函数调用,如果在函数体内,则只能由该函数调用。

    结构体的声明方法如下:

    struct StcTest{};

    然后在结构体内(花括号内)声明该结构体的成员变量,如下:

    struct StcTest{

        string a;

        int b;

        float c;

        char d;

    };

    在使用结构体前,需要创建结构体的实体,如下:

    StcTest stcTest;

    然后才可以访问结构体的成员变量,如下:

    string name = stcTest.a;

    int score = stcTest.b;

    ......

    在创建结构体的实体时,可以对结构体的成员变量按顺序进行赋值,如:

    StcTest stcTest1 = {"a", 1, 3.5f, 'b'};

    StcTest stcTest2{"a", 1, 3.5f, 'b'};

    StcTest stcTest3{ };

    示例:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
struct Stc1{
public:
	string name = "Haze";
	float height = 176.8f;
	float weight = 74.0f;
	string job = "Programmer";
};

int main()
{
	Stc1 stc;
	cout << stc.name << "'s height is " 
		<< stc.height << "cm and his weight is " 
		<< stc.weight << "kg and he is a " << stc.job << endl;
	Stc1 stc2;
	stc2.name = "YH";
	stc2.height = 177.0f;
	stc2.weight = 74.0f;
	stc2.job = "Technical artist";
	cout << stc2.name << "'s height is "
		<< stc2.height << "cm and his weight is "
		<< stc2.weight << "kg and he is a " << stc2.job << endl;

}

    可以声明结构体数组,其方法和其他输入类似,如下:

    StcTest stcTest[]{{}, {}, {}, {}};

    因为结构体数组的元素均为一个结构体,所以每个元素都需要用花括号括起来。

[展开全文]
YH1209 · 2020-08-17 · 0

C++string类库

    使用string类库需要include<string>。引入类库后可直接使用string声明一个字符串,如:

    string str;

    string str2 = "Example";

    未声明的字符串会默认为空字符串。可以对字符串类型的变量赋值,如:

    string str;

    string str1 = "Example";

    str = str1;

    获取用户输入的字符串赋值给字符串变量需要使用getline()方法,如下:

    string str;

    getline(cin,str);

    获取字符串中的第i个字符的操作如下:

    string str = "Example";

    char c = str[2];

    注意,i不能越界,必须大于0且小于字符串的长度。

    字符串变量之间可以进行加法运算,通过"+"运算符连接两个字符串,如:

    str1 = "Example";

    str2 = " string";

    string str3 = str1 + str2;

    str3的值为"Example string"。

    可以通过string.size()获取字符的个数。

[展开全文]
YH1209 · 2020-08-14 · 0

使用cin函数需要注意的点

    用户输入的制表符、空格都会被cin视为输入的结束。

    可以使用cin.getline(array, value);来避免上述问题。

[展开全文]
YH1209 · 2020-08-14 · 0