c++基础全套学习笔记(入门)

1. helloword入门与注释

1)helloword入门

#include <iostream>
using namespace std;
//main函数
int main()
{
    cout << "hello world" ;
    system("pause");
    return 0;
}

2)注释
单行注释// 描述信息

// main函数
int main()
……

多行注释/* 描述信息 */

/*
 main函数
 这是一个入口函数
*/
int main()
……

2. 变量与常量

1)变量

数据类型 变量名 = 初始值;

int a = 10;
std::cout << "a = " << a << std::endl;

2)常量
① 宏常量

#define 常量名 常量值

#define week 7

② const常量

const 数据类型 常量名 = 常量值

const int month = 12;

3. 关键字与标识符

1)C++关键字如下,在定义变量或者常量时候,不要用关键字:

asmdoifreturntypedef
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizeofunion
caseenummutablestaticunsigned
catchexplicitnamespacestatic_castusing
charexportnewstructvirtual
classexternoperatorswitchvoid
constfalseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrowwhile
defaultfriendregistertrue
deletegotoreinterpret_casttry

sizeof关键字

作用是统计数据类型或变量所占的内存大小

[root@hadoop100 cpplearning]# cat sizeof.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        long long l = 0;

        cout << sizeof(l) << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ sizeof.cpp -o sizeof
[root@hadoop100 cpplearning]# ./sizeof
8

2)标识符命名规则

  • 标识符不能是关键字
  • 标识符只能由字母、数字、下划线组成
  • 第一个字符必须为字母或下划线
  • 标识符中字母区分大小写

数据输入

[root@hadoop100 cpplearning]# cat cin.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        string str;
        cout << "please input a string: " << endl;
        cin >> str;
        cout << "your input is: " << str << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ cin.cpp -o cin
[root@hadoop100 cpplearning]# ./cin
please input a string:
China
your input is: China
[root@hadoop100 cpplearning]#

4. 数据类型

1)整型

整型变量表示的是整数类型的数据,区别在于所占内存空间不同

数据类型占用空间取值范围
short(短整型)2字节-32768~32677(-2^15 ~ 2^15-1)
int(整型)4字节-21亿~21亿 (-2^31 ~ 2^31-1)
long(长整形)Windows为4字节,Linux为4字节(32位),8字节(64位)-21亿~21亿 (-2^31 ~ 2^31-1)
long long(长长整形)8字节-922亿亿~922亿亿(-2^63 ~ 2^63-1)

2)浮点型(实型)

数据类型占用空间有效数字范围定义方式
float4字节7位有效数字float f1 = 3.14f;
double8字节15~16位有效数字double d1 = 3.14;

科学计数法:

int main() {
	
	float f = 3e2; // 3 * 10 ^ 2 
	cout << "f = " << f << endl;

	float f1 = 3e-2;  // 3 * 0.1 ^ 2
	cout << "f1 = " << f1 << endl;

	system("exit 0");

	return 0;
}

3)字符型

使用单引号,表示单个字符,占用一个字节,将对应的ASCII编码放入到存储单元

char c = ‘a’;

查看ASCII码值

[root@hadoop100 cpplearning]# cat ascii.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        char c = 'a';

        cout << int(c) << endl;
        cout << int('b') << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ ascii.cpp -o ascii
[root@hadoop100 cpplearning]# ./ascii
97
98

4)字符串型

表示一串字符

  1. C风格字符串:用双引号括起来

char 变量名[] = “字符串值”

int main() {

	char str1[] = "hello world";
	cout << str1 << endl;
    
	system("exit 0");

	return 0;
}
  1. C++风格字符串: 需要加入头文件==#include<string>==

string 变量名 = “字符串值”

int main() {

	string str = "hello world";
	cout << str << endl;
	
	system("exit 0");

	return 0;
}
[root@hadoop100 cpplearning]# cat string.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {

        char chs[] = "I'm chars";
        cout << chs << endl;

        string str = "I'm a string";
        cout << str << endl;

        cout << sizeof(chs) << endl;
        cout << sizeof(str) << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ string.cpp -o string
[root@hadoop100 cpplearning]# ./string
I'm chars
I'm a string
10
8

5)布尔型

bool类型只有两个值,占1个字节大小:

  • true — 真(本质是1)
  • false — 假(本质是0)
[root@hadoop100 cpplearning]# cat bool.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        bool b = true;
        cout << b << endl;

        cout << sizeof(b) << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ bool.cpp -o bool
[root@hadoop100 cpplearning]# ./bool
1
1

5. 运算符

1)算数运算符

用于处理四则运算 :

运算符术语示例结果
+正号+33
-负号-3-3
+10 + 515
-10 - 55
*10 * 550
/10 / 52
%取模(取余)10 % 31
++前置递增a=2; b=++a;a=3; b=3;
++后置递增a=2; b=a++;a=3; b=2;
前置递减a=2; b=–a;a=1; b=1;
后置递减a=2; b=a–;a=1; b=2;

示例1:

2) 赋值运算符

用于将表达式的值赋给变量

运算符术语示例结果
=赋值a=2; b=3;a=2; b=3;
+=加等于a=0; a+=2;a=2;
-=减等于a=5; a-=3;a=2;
*=乘等于a=2; a*=2;a=4;
/=除等于a=4; a/=2;a=2;
%=模等于a=3; a%2;a=1;

3)比较运算符

用于表达式的比较,并返回一个1(真值)或0(假值)

运算符术语示例结果
==相等于4 == 30
!=不等于4 != 31
<小于4 < 30
>大于4 > 31
<=小于等于4 <= 30
>=大于等于4 >= 11
[root@hadoop100 cpplearning]# cat compare.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a = 1;
        int b = 2;
        cout << (a >= b) << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ compare.cpp -o compare
[root@hadoop100 cpplearning]# ./compare
0

4)逻辑运算符

用于根据表达式的值返回1(真值)或0(假值)

运算符术语示例结果
!!a如果a为假,则!a为真; 如果a为真,则!a为假。
&&a && b如果a和b都为真,则结果为真,否则为假。
||a || b如果a和b有一个为真,则结果为真,二者都为假时,结果为假。
[root@hadoop100 cpplearning]# cat logic.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        bool b = false;
        cout << (! b) << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ logic.cpp -o logic
[root@hadoop100 cpplearning]# ./logic
1

6. 程序流程结构

1)选择结构–if判断

几种种模式:

if 模式
if…else… 模式
if…else if… 模式
if…else if…else 模式
嵌套if模式

[root@hadoop100 cpplearning]# cat if.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a = 100;

        if (a>50)
        {
            cout << "a > 50" << endl;
        }


        if (a > 100)
        {
            cout << "a > 100" << endl;
        } else
        {
           cout << "a <= 100" << endl;
        }


        if (a > 100)
        {
            cout << "a > 100" << endl;
        } else if(a >= 50)
        {
           cout << "a >= 50" << endl;
        } else
        {
           cout << "a < 50" << endl;
        }


        if (a > 200)
        {
            cout << "a > 100" << endl;
        } else if(a <= 150)
        {
           if (a <= 100)
           {
              cout << "a <= 100" << endl;
           }
        }

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ if.cpp -o if
[root@hadoop100 cpplearning]# ./if
a > 50
a <= 100
a >= 50
a <= 100

2)选择结构–三目运算符

表达式1 ? 表达式2 :表达式3

结果为一个变量,可以继续赋值

[root@hadoop100 cpplearning]# cat ifs.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a = 100;
        int b = 200;
        int c;

        c = (a > b ? a :b);
        cout << c << endl;
        
        // 结果为一个变量,可以继续赋值
        (a > b ? a :b) = 300;
        cout << b << endl;
        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ ifs.cpp -o ifs
[root@hadoop100 cpplearning]# ./ifs
200
300

3)选择结构–switch

表达式只能是整型或者字符型,效率比switch高

switch(表达式)

{

case 结果1:执行语句;break;

case 结果2:执行语句;break;

default:执行语句;break;

}

[root@hadoop100 cpplearning]# cat switch.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a;

        cout << "plz input a number: " << endl;
        cin >> a;

        switch(a)
        {
          case 10:
             cout << "a = 10" << endl;
             break;
          case 20:
             cout << "a = 20" << endl;
             break;
          case 30:
             cout << "a = 30" << endl;
             break;
          default:
             cout << "a not in [10, 20, 30]" << endl;
        }
        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ switch.cpp -o switch
[root@hadoop100 cpplearning]# ./switch
plz input a number:
20
a = 20
[root@hadoop100 cpplearning]# ./switch
plz input a number:
50
a not in [10, 20, 30]

4)循环结构–while

先判断后执行

while(循环条件)
{
循环语句
}

[root@hadoop100 cpplearning]# cat while.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a = 10;
        while(a > 0)
        {
          cout << a << " ";
          a--;
        }

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ while.cpp -o while
[root@hadoop100 cpplearning]# ./while
10 9 8 7 6 5 4 3 2 1 

5)循环结构–dowhile

先执行后判断

do
{
循环语句
} while (循环条件) ;

[root@hadoop100 cpplearning]# cat dowhile.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

        int a = 10;
        do
        {
          cout << a << " ";
          a--;
        } while(a > 0);

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ dowhile.cpp -o dowhile
[root@hadoop100 cpplearning]# ./dowhile
10 9 8 7 6 5 4 3 2 1 

6)循环结构–for

for(起始表达式;条件表达式;末尾循环体)
{
循环语句;
}

[root@hadoop100 cpplearning]# cat for.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    for(int i = 10;i > 0;i--)
    {
        cout << i << " ";
    }
    cout << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ for.cpp -o for
[root@hadoop100 cpplearning]# ./for
10 9 8 7 6 5 4 3 2 1

7)流程控制关键字–break,continue与goto

break退出:

[root@hadoop100 cpplearning]# cat for.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    for(int i = 10;i > 0;i--)
    {
        cout << i << " ";
        if(i == 5){
            break;
        }
    }
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ for.cpp -o for
[root@hadoop100 cpplearning]# ./for
10 9 8 7 6 5 

continue跳过:

[root@hadoop100 cpplearning]# cat for.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    for(int i = 10;i > 0;i--)
    {
        if(i == 5){
            continue;
        }
        cout << i << " ";
    }
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ for.cpp -o for
[root@hadoop100 cpplearning]# ./for
10 9 8 7 6 4 3 2 1 

goto跳转(不推荐使用):

[root@hadoop100 cpplearning]# cat goto.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    cout << 1 << endl;
    cout << 2 << endl;
    cout << 3 << endl;

    goto F;
    cout << 4 << endl;
    cout << 5 << endl;
    cout << 6 << endl;

    F:
    cout << 7 << endl;
    cout << 8 << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ goto.cpp -o goto
[root@hadoop100 cpplearning]# ./goto
1
2
3
7
8

7. 数组

1. 一维数组

1)指定长度,先定义后赋值

访问未赋值的索引会得到不可预测的值

数据类型 数组名[ 数组长度 ];
数组名[ 0 ] = value0;
数组名[ 1 ] = value1;
数组名[ 2 ] = value2;
……

[root@hadoop100 cpplearning]# cat arr.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    int arr[5];
    arr[0] = 0;
    arr[1] = 1;
    arr[2] = 2;
    arr[3] = 3;

    cout << arr[3] <<endl;
    cout << arr[4] <<endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ arr.cpp -o arr
[root@hadoop100 cpplearning]# ./arr
3
1918405216
[root@hadoop100 cpplearning]# ./arr
3
-118953088
[root@hadoop100 cpplearning]# ./arr
3
1628071280
2)指定长度,定义同时赋值

未赋值会被置位默认值

数据类型 数组名[ 数组长度 ] = { 值1,值2 …};

[root@hadoop100 cpplearning]# cat arr.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    int arr[5] = {1, 2, 3, 4};
    cout << arr[3] <<endl;
    cout << arr[4] <<endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ arr.cpp -o arr
[root@hadoop100 cpplearning]# ./arr
4
0
3)不指定长度,定义同时赋值

数据类型 数组名[ ] = { 值1,值2 …};

越界不报错

[root@hadoop100 cpplearning]# cat arr.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4};
    cout << arr[3] <<endl;
    cout << arr[4] <<endl;
    cout << arr[10] <<endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ arr.cpp -o arr
[root@hadoop100 cpplearning]# ./arr
4
0
2088179912
4)数组长度与首地址

数组的size与一个元素的size相除即为数组的长度,即数组中元素的个数
数组名(是一个常量)就是数组的第一个元素的地址

[root@hadoop100 cpplearning]# cat arr.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int arrSize = sizeof(arr);
    int elementSize = sizeof(arr[0]);
    int arrLength = arrSize / elementSize;
    cout <<  arrSize << " " << elementSize << " "  << arrLength << endl;
    cout << "数组第一个元素的地址为:" << arr << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ arr.cpp -o arr
[root@hadoop100 cpplearning]# ./arr
24 4 6
数组第一个元素的地址为:0x7ffc19134c90

2. 二维数组

数据类型 数组名[ 行数 ][ 列数 ];

	int arr[2][3];
	arr[0][0] = 1;
	arr[0][1] = 2;
	arr[0][2] = 3;
	arr[1][0] = 4;
	arr[1][1] = 5;
	arr[1][2] = 6;

数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };

int arr2[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};

int arr3[2][3] = { 1,2,3,4,5,6 }; 

数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};

int arr4[][3] = { 1,2,3,4,5,6 };
2)二维数组长度与首地址
int main() {

	int arr[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	cout << "数组大小: " << sizeof(arr) << endl;
	cout << "一行大小: " << sizeof(arr[0]) << endl;
	cout << "一个元素大小: " << sizeof(arr[0][0]) << endl;

	cout << "行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
	cout << "列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

	//地址
	cout << "首地址:" << arr << endl;
	cout << "第一行地址:" << arr[0] << endl;
	cout << "第一行第一个元素地址:" << &arr[0][0] << endl;

	system("exit 0");

	return 0;
}
[root@hadoop100 cpplearning]# g++ arr2.cpp -o arr2
[root@hadoop100 cpplearning]# ./arr2
数组大小: 24
一行大小: 12
一个元素大小: 4
行数: 2
列数: 3
首地址:0x7ffdb81818e0
第一行地址:0x7ffdb81818e0
第一行第一个元素地址:0x7ffdb81818e0

8. 函数

1. 函数定义

  1. 有参有返

返回值类型 函数名 (参数列表)
{
函数体语句
return表达式
}

int add(int a, int b)
{
   return a + b;
}

2)无参无返

void test()
{
	cout << "this is test01" << endl;
}

3)无参有返

int test()
{
	cout << "this is test" << endl;
	return 10;
}
  1. 有参无返
void test(int a)
{
	cout << "a = " << a << endl;
}

2. 函数调用

[root@hadoop100 cpplearning]# cat function.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;


int add(int a, int b)
{
   return a + b;
}

int main() {
    cout << add(22,33) << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ function.cpp -o function
[root@hadoop100 cpplearning]# ./function
55

3. 函数声明

函数调用在函数定义之前时,需要在调用之前先进行函数声明。
声明可以写多次,定义只能写一次

返回值类型 函数名 (参数列表);

[root@hadoop100 cpplearning]# cat function.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;


int add(int a, int b);

int main() {

        cout << add(22,33) << endl;
        system("exit 0");

        return 0;
}

int add(int a, int b)
{
   return a + b;
}
[root@hadoop100 cpplearning]# g++ function.cpp -o function
[root@hadoop100 cpplearning]# ./function
55

4. 函数分文件编写

1.创建后缀名为.h的头文件 ,写函数的声明
2.创建后缀名为.cpp的源文件,在源文件include头文件,中写函数的定义
3.在调用的cpp文件内include头文件,然后调用

注意:编译时要同时编译调用的cpp文件与源文件

[root@hadoop100 cpplearning]# cat function.cpp
#include "function.h"
int add(int a, int b)
{
   return a + b;
}

[root@hadoop100 cpplearning]# cat functest.cpp
#include "function.h"
int main()
{
    int a = 100;
    int b = 200;
    cout << add(a,b) <<endl;

    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# cat function.h
#include <iostream>
#include <stdlib.h>
using namespace std;

int add(int a, int b);
[root@hadoop100 cpplearning]# g++ functest.cpp function.cpp -o function
[root@hadoop100 cpplearning]# ./function
300

9. 指针

指针是一个地址,
所有指针类型在32位操作系统下是4个字节
所有指针类型在64位操作系统下是8个字节

1)变量取址–&

int a = 10;
cout << "a的地址为:" << &a << endl;

a的地址为:0x7fffccd0a700

2)指针定义

数据类型 * 变量名;

[root@hadoop100 cpplearning]# cat point.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {

        int num = 10;
        int * p = &num;
        cout << p << endl;

        system("exit 0");

        return 0;
}
[root@hadoop100 cpplearning]# g++ point.cpp -o point
[root@hadoop100 cpplearning]# ./point
0x7ffc5f8b2044

3)使用指针(解引用)

‘*指针变量’ 代表 指针变量所指的变量

[root@hadoop100 cpplearning]# cat point.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {

    int num = 10;
    int * p = &num;
    *p = 20;
    cout << num << endl;

    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ point.cpp -o point
[root@hadoop100 cpplearning]# ./point
20

4)空指针与野指针

空指针:指针变量指向内存中编号为0的空间
野指针:指针变量指向非法的内存空间

int main() {

	//定义空指针
	int * p = NULL;

	//访问空指针报错 
	//内存编号0 ~255为系统占用内存,不允许用户访问
	cout << *p << endl;

   //指针变量p指向内存地址编号为0x1100的空间
	int * p1 = (int *)0x1100;
	//访问野指针报错 
	cout << *p1 << endl;
	
	system("pause");
	return 0;
}

5)const 修饰指针

① 常量指针:const修饰指针,指针指向可以改,指针指向的值不可以更改

② 指针常量:const修饰常量,指针指向不可以改,指针指向的值可以更改

③ 常量指针常量:const修饰常量又修饰指针,指针指向和指针指向的值都不可以更改

[root@hadoop100 cpplearning]# cat const.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {

    int num = 10;
    int num1 = 20;
    const int * p = &num;
    p = &num1;
    // *p = 30;  错误:向只读位置‘* p’赋值
    cout << *p << endl;

    int * const p1 = &num;
    *p1 = 100;
    // p1 = &num1;  错误:向只读变量‘p1’赋值
    cout << num << endl;

    const int * const p2 = &num;
    // *p2 = 100;  错误:向只读位置‘*(const int*)p2’赋值
    // p2 = &num1;  错误:向只读变量‘p2’赋值

    cout << *p2 << endl;

    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ const.cpp -o const
[root@hadoop100 cpplearning]# ./const
20
100
100

6)指针访问数组

[root@hadoop100 cpplearning]# cat pointArr.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {

    int arr[] = {11,22,33,44,55,66,77};
    int * p = arr; // 数组名即首地址
    for(int i = 0;i < 7;i++)
    {
        cout << *p << " ";
        p++; //因为p为int型指针,加1即地址加4个字节
    }

    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ pointArr.cpp -o pointarr
[root@hadoop100 cpplearning]# ./pointarr
11 22 33 44 55 66 77 [

7)指针作为函数形参进行地址传递

*会改变实参的值

[root@hadoop100 cpplearning]# cat pointFunc.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void swap(int * p1,int * p2)
{
   int  tmp = *p1;
   *p1 = *p2;
   *p2 = tmp;
}

int main() {

    int a = 10;
    int b = 20;
    swap(&a, &b);

    cout << a << " " << b << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ pointFunc.cpp -o pointf
[root@hadoop100 cpplearning]# ./pointf
20 10

8)指针、数组、函数实现冒泡排序

[root@hadoop100 cpplearning]# cat pupple.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void swap(int * a, int * b){
   int tmp = *a;
   *a = *b;
   *b = tmp;
}

int main() {

    int arr[] = {2,4,3,5,1,6,3,7,3,1,90,44,23};

    int len = sizeof(arr) / sizeof(arr[0]);

    for(int i = 0;i < len; i++){
      int tmp = arr[0];
      for(int j = 0; j< len - 1 - i;j++){
          if(*(arr + j) > *(arr + j + 1)){
              swap(arr + j,arr  + j + 1);
          }

      }

    }
    for(int i = 0;i < len; i++){
      cout << arr[i] << " ";
    }
    cout << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ pupple.cpp -o pupple
[root@hadoop100 cpplearning]# ./pupple
1 1 2 3 3 3 4 5 6 7 23 44 90

10. 结构体

1)定义、实例化、成员访问

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型

struct 结构体名
{
结构体成员列表
};

实例化:(struct关键字可省略)

  • struct 结构体名 变量名;
  • struct 结构体名 变量名 = { 成员1值 , 成员2值…};
  • 定义结构体时顺便创建变量;

访问结构体成员:

实例名.成员名

[root@hadoop100 cpplearning]# cat struct.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

struct Student{
    string name;
    int age;
    int score;
} stu;
int main() {

    struct Student stu1;
    stu1.name = "name1";
    stu1.age = 16;
    stu1.score = 78;

    struct Student stu2 = {"name2", 18, 87};
    stu.name = "name";
    stu.age = 17;
    stu.score = 99;

    cout << stu.name << " " << stu.age  << " " << stu.score << endl;
    cout << stu1.name << " " << stu1.age  << " " << stu1.score << endl;
    cout << stu2.name << " " << stu2.age  << " " << stu2.score << endl;
    cout << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ struct.cpp -o struct
[root@hadoop100 cpplearning]# ./struct
name 17 99
name1 16 78
name2 18 87

2)结构体数组

[root@hadoop100 cpplearning]# cat struct.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

struct Student{
    string name;
    int age;
    int score;
};
int main() {

    Student stus[3] =
    {
      {"name1",12,78},
      {"name2",13,76},
      {"name3",11,98}
    };

    cout << stus[0].name << " " << stus[1].age  << " " << stus[2].score << endl;
    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ struct.cpp -o struct
[root@hadoop100 cpplearning]# ./struct
name1 13 98

3)结构体指针

利用操作符 -> 可以通过结构体指针访问结构体属性

[root@hadoop100 cpplearning]# cat struct.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

struct Student{
    string name;
    int age;
    int score;
};
int main() {

    Student s =  {"name1",12,78};
    Student * p = &s;
    cout <<  p->name << " " << p->score  << endl;

    system("exit 0");
    return 0;
}
[root@hadoop100 cpplearning]# g++ struct.cpp -o struct
[root@hadoop100 cpplearning]# ./struct
name1 78

3)结构体指针做函数参数

指针只占4字节,比变量做形参省空间
形参加const,防止外部变量(实参)被修改

//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

//const使用场景
void printStudent(const student *stu) //加const防止函数体中的误操作
{
	//stu->age = 100; //操作失败,因为加了const修饰
	cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;

}

int main() {

	student stu = { "张三",18,100 };

	printStudent(&stu);

	system("pause");

	return 0;
}
  • 27
    点赞
  • 191
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维小菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值