1.数组初始化
int a[5]={10,20,30,40,50};
如果没写个数,编译器自己数,数到几个算几个。
int a[]={10,20,30,40,50};
初始化数据不够,后面一律补0.
int a[10]={10,20,30,40,50};
#include <iostream>
using namespace std;
int main()
{
int a[10]={10,20,30,40,50};
for(int i=0;i<10;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
10 20 30 40 50 0 0 0 0 0
2.数组大小
#include <iostream>
using namespace std;
int main()
{
int a[5]={10,20,30,40,50};
cout<<"sizeof(int):"<<sizeof(int)<<endl;
cout<<"sizeof(a):"<<sizeof(a)<<endl;
cout<<"sizeof(int [5]):"<<sizeof(int [5])<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
sizeof(int):4
sizeof(a):20
sizeof(int [5]):20
2.数组清零
2.1.循环赋值
int a[5]={10,20,30,40,50};
for(int i=0; i<5; i++)
{
a[i] = 0;
}
2.2.memset()
#include <iostream>
using namespace std;
#include <string.h> //memset头文件
int main()
{
int a[5]={10,20,30,40,50};
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
memset(a,0,sizeof(a));
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
10 20 30 40 50
0 0 0 0 0
2.3. bzero()
#include <iostream>
using namespace std;
#include <string.h> //bzero头文件
int main()
{
int a[5]={10,20,30,40,50};
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
bzero(a, sizeof(a));
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
10 20 30 40 50
0 0 0 0 0
3.字符变量地址特殊处理
为了与C语言兼容,C++在输出字符变量地址时,改成输出从这个内存单元开始存放的一系列字符,直到向后遇到一个“\0”字符为止。
#include <iostream>
using namespace std;
int main()
{
char ch='A';
cout<<"&ch="<<&ch<<endl;
char name[5]={'a','b','c','\0','d'};
cout<<"name="<<name<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# ./a.out
&ch=AN
name=abc
那如何看字符变量地址呢?
通过强制类型转换实现。
#include <iostream>
using namespace std;
int main()
{
char ch='A';
cout<<"&ch="<<hex<<"0x"<<(long)&ch<<endl;
char name[5]={'a','b','c','\0','d'};
cout<<"name="<<hex<<"0x"<<(long)name<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
&ch=0x7fff7b733e12
name=0x7fff7b733e13
4.排序
4.1.选择排序
#include <iostream>
using namespace std;
// void change(int &m,int &n)
// {
// int temp = m;
// m = n;
// n = temp;
// }
//打印数组内容
void show(int num[],int n)
{
for(int i=0;i<n;i++)
{
cout<<num[i]<<' ';
}
cout<<endl;
}
int main()
{
int num[10] = {20,30,10,5,8,70,60,80,65,85};
cout<<"排序前:";
show(num,10); //打印排序前数组内容
for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{
if(num[j]>num[i])
{
//交换两个数据
swap(num[j],num[i]);
//change(num[j],num[i]);
}
}
}
cout<<"排序后:";
show(num,10); //打印排序后数组内容
return 0;
}
执行结果:
root@host:/home/LinuxShare/004.c++/day05# g++ main.cpp
root@host:/home/LinuxShare/004.c++/day05# ./a.out
排序前:20 30 10 5 8 70 60 80 65 85
排序后:85 80 70 65 60 30 20 10 8 5
5.快速打字法
root@host:/home/LinuxShare# cat ~/.exrc
set ts=4
set nu
ab .i #include <iostream>
ab .u using namespace std;
ab .m int main()
ab .f #include <fstream>
ab .s #include <string>
ab .r return
ab #i #include
使用方法,在使用vi编辑时:
输入.i回车,就会出现#include