1)helloword入门
#include
using namespace std;
//main函数
int main()
{
cout << "hello world" ;
system("pause");
return 0;
}
2)注释
① 单行注释:// 描述信息
// main函数
int main()
……
② 多行注释: /* 描述信息 */
/*
main函数
这是一个入口函数
*/
int main()
……
1)变量
数据类型 变量名 = 初始值;
int a = 10;
std::cout << "a = " << a << std::endl;
2)常量
① 宏常量
#define 常量名 常量值
#define week 7
② const常量
const 数据类型 常量名 = 常量值
const int month = 12;
1)C++关键字如下,在定义变量或者常量时候,不要用关键字:
asm | do | if | return | typedef |
---|---|---|---|---|
auto | double | inline | short | typeid |
bool | dynamic_cast | int | signed | typename |
break | else | long | sizeof | union |
case | enum | mutable | static | unsigned |
catch | explicit | namespace | static_cast | using |
char | export | new | struct | virtual |
class | extern | operator | switch | void |
const | false | private | template | volatile |
const_cast | float | protected | this | wchar_t |
continue | for | public | throw | while |
default | friend | register | true | |
delete | goto | reinterpret_cast | try |
作用是统计数据类型或变量所占的内存大小
[root@hadoop100 cpplearning]# cat sizeof.cpp
#include
#include
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
#include
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]#
整型变量表示的是整数类型的数据,区别在于所占内存空间不同:
数据类型 | 占用空间 | 取值范围 |
---|---|---|
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) |
数据类型 | 占用空间 | 有效数字范围 | 定义方式 |
---|---|---|---|
float | 4字节 | 7位有效数字 | float f1 = 3.14f; |
double | 8字节 | 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;
}
使用单引号,表示单个字符,占用一个字节,将对应的ASCII编码放入到存储单元
char c = ‘a’;
查看ASCII码值
[root@hadoop100 cpplearning]# cat ascii.cpp
#include
#include
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
表示一串字符
char 变量名[] = “字符串值”
int main() {
char str1[] = "hello world";
cout << str1 << endl;
system("exit 0");
return 0;
}
string 变量名 = “字符串值”
int main() {
string str = "hello world";
cout << str << endl;
system("exit 0");
return 0;
}
[root@hadoop100 cpplearning]# cat string.cpp
#include
#include
#include
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
bool类型只有两个值,占1个字节大小:
[root@hadoop100 cpplearning]# cat bool.cpp
#include
#include
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
用于处理四则运算 :
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
+ | 正号 | +3 | 3 |
- | 负号 | -3 | -3 |
+ | 加 | 10 + 5 | 15 |
- | 减 | 10 - 5 | 5 |
* | 乘 | 10 * 5 | 50 |
/ | 除 | 10 / 5 | 2 |
% | 取模(取余) | 10 % 3 | 1 |
++ | 前置递增 | 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:
用于将表达式的值赋给变量
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
= | 赋值 | 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; |
用于表达式的比较,并返回一个1(真值)或0(假值)
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
== | 相等于 | 4 == 3 | 0 |
!= | 不等于 | 4 != 3 | 1 |
< | 小于 | 4 < 3 | 0 |
> | 大于 | 4 > 3 | 1 |
<= | 小于等于 | 4 <= 3 | 0 |
>= | 大于等于 | 4 >= 1 | 1 |
[root@hadoop100 cpplearning]# cat compare.cpp
#include
#include
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
用于根据表达式的值返回1(真值)或0(假值)
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
! | 非 | !a | 如果a为假,则!a为真; 如果a为真,则!a为假。 |
&& | 与 | a && b | 如果a和b都为真,则结果为真,否则为假。 |
|| | 或 | a || b | 如果a和b有一个为真,则结果为真,二者都为假时,结果为假。 |
[root@hadoop100 cpplearning]# cat logic.cpp
#include
#include
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
几种种模式:
if 模式
if…else… 模式
if…else if… 模式
if…else if…else 模式
嵌套if模式
[root@hadoop100 cpplearning]# cat if.cpp
#include
#include
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
表达式1 ? 表达式2 :表达式3
结果为一个变量,可以继续赋值
[root@hadoop100 cpplearning]# cat ifs.cpp
#include
#include
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
表达式只能是整型或者字符型,效率比switch高
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
…
default:执行语句;break;
}
[root@hadoop100 cpplearning]# cat switch.cpp
#include
#include
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]
先判断后执行
while(循环条件)
{
循环语句
}
[root@hadoop100 cpplearning]# cat while.cpp
#include
#include
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
先执行后判断
do
{
循环语句
} while (循环条件) ;
[root@hadoop100 cpplearning]# cat dowhile.cpp
#include
#include
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
for(起始表达式;条件表达式;末尾循环体)
{
循环语句;
}
[root@hadoop100 cpplearning]# cat for.cpp
#include
#include
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
break退出:
[root@hadoop100 cpplearning]# cat for.cpp
#include
#include
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
#include
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
#include
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
访问未赋值的索引会得到不可预测的值
数据类型 数组名[ 数组长度 ];
数组名[ 0 ] = value0;
数组名[ 1 ] = value1;
数组名[ 2 ] = value2;
……
[root@hadoop100 cpplearning]# cat arr.cpp
#include
#include
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
未赋值会被置位默认值
数据类型 数组名[ 数组长度 ] = { 值1,值2 …};
[root@hadoop100 cpplearning]# cat arr.cpp
#include
#include
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
数据类型 数组名[ ] = { 值1,值2 …};
越界不报错
[root@hadoop100 cpplearning]# cat arr.cpp
#include
#include
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
数组的size与一个元素的size相除即为数组的长度,即数组中元素的个数
数组名(是一个常量)就是数组的第一个元素的地址
[root@hadoop100 cpplearning]# cat arr.cpp
#include
#include
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
数据类型 数组名[ 行数 ][ 列数 ];
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 };
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
返回值类型 函数名 (参数列表)
{
函数体语句
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;
}
void test(int a)
{
cout << "a = " << a << endl;
}
[root@hadoop100 cpplearning]# cat function.cpp
#include
#include
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
函数调用在函数定义之前时,需要在调用之前先进行函数声明。
声明可以写多次,定义只能写一次
返回值类型 函数名 (参数列表);
[root@hadoop100 cpplearning]# cat function.cpp
#include
#include
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
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
#include
using namespace std;
int add(int a, int b);
[root@hadoop100 cpplearning]# g++ functest.cpp function.cpp -o function
[root@hadoop100 cpplearning]# ./function
300
指针是一个地址,
所有指针类型在32位操作系统下是4个字节
所有指针类型在64位操作系统下是8个字节
int a = 10;
cout << "a的地址为:" << &a << endl;
a的地址为:0x7fffccd0a700
数据类型 * 变量名;
[root@hadoop100 cpplearning]# cat point.cpp
#include
#include
#include
using namespace std;
int main() {
int num = 10;
int * p = #
cout << p << endl;
system("exit 0");
return 0;
}
[root@hadoop100 cpplearning]# g++ point.cpp -o point
[root@hadoop100 cpplearning]# ./point
0x7ffc5f8b2044
‘*指针变量’ 代表 指针变量所指的变量
[root@hadoop100 cpplearning]# cat point.cpp
#include
#include
#include
using namespace std;
int main() {
int num = 10;
int * p = #
*p = 20;
cout << num << endl;
system("exit 0");
return 0;
}
[root@hadoop100 cpplearning]# g++ point.cpp -o point
[root@hadoop100 cpplearning]# ./point
20
空指针:指针变量指向内存中编号为0的空间
野指针:指针变量指向非法的内存空间
int main() {
//定义空指针
int * p = NULL;
//访问空指针报错
//内存编号0 ~255为系统占用内存,不允许用户访问
cout << *p << endl;
//指针变量p指向内存地址编号为0x1100的空间
int * p1 = (int *)0x1100;
//访问野指针报错
cout << *p1 << endl;
system("pause");
return 0;
}
① 常量指针:const修饰指针,指针指向可以改,指针指向的值不可以更改
② 指针常量:const修饰常量,指针指向不可以改,指针指向的值可以更改
③ 常量指针常量:const修饰常量又修饰指针,指针指向和指针指向的值都不可以更改
[root@hadoop100 cpplearning]# cat const.cpp
#include
#include
#include
using namespace std;
int main() {
int num = 10;
int num1 = 20;
const int * p = #
p = &num1;
// *p = 30; 错误:向只读位置‘* p’赋值
cout << *p << endl;
int * const p1 = #
*p1 = 100;
// p1 = &num1; 错误:向只读变量‘p1’赋值
cout << num << endl;
const int * const p2 = #
// *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
[root@hadoop100 cpplearning]# cat pointArr.cpp
#include
#include
#include
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 [
*会改变实参的值
[root@hadoop100 cpplearning]# cat pointFunc.cpp
#include
#include
#include
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
[root@hadoop100 cpplearning]# cat pupple.cpp
#include
#include
#include
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
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
struct 结构体名
{
结构体成员列表
};
实例化:(struct关键字可省略)
- struct 结构体名 变量名;
- struct 结构体名 变量名 = { 成员1值 , 成员2值…};
- 定义结构体时顺便创建变量;
访问结构体成员:
实例名.成员名
[root@hadoop100 cpplearning]# cat struct.cpp
#include
#include
#include
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
[root@hadoop100 cpplearning]# cat struct.cpp
#include
#include
#include
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
利用操作符 ->
可以通过结构体指针访问结构体属性
[root@hadoop100 cpplearning]# cat struct.cpp
#include
#include
#include
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
指针只占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;
}