1,memset( )函数
头文件:#include <cstring>
作用:用于初始化
使用:memset(a,num,n);
'a'表示数组名
'num'表示初始化赋予的值
'n'表示长度
注意:初始化赋值是按每个字节赋值
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int a[6];
memset(a,2,24);
for(int i=0;i<6;i++){
cout <<a[i]<<' ';
}
return 0;
}
输出:33686018 33686018 33686018 33686018 33686018 33686018
因为是给字节赋值,所以变成了这样
特殊的两个,赋予0
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int a[6];
memset(a,0,24);
for(int i=0;i<6;i++){
cout <<a[i]<<' ';
}
return 0;
}
输出:0 0 0 0 0 0
赋予-1
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int a[6];
memset(a,-1,24);
for(int i=0;i<6;i++){
cout <<a[i]<<' ';
}
return 0;
}
输出-1 -1 -1 -1 -1 -1
1Byte(字节)=8bit(二进制的那个,01)
1KB=1024Byte
1MB=1024KB
1Mb=1/8MB
int 32位 4Byte
通常这么写 memset(a,0,sizeof a);
2,memcpy( )
作用:赋值数组
使用:memcpy(b,a,n);
'b'表示新数组
'a'表示原数组
'n'表示长度 字节单位
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int a[6]={0,1,2,3,4,5};
int b[6];
memcpy(b,a,sizeof a);
for(int i=0;i<6;i++){
cout <<b[i]<<' ';
}
return 0;
}
输出:0 1 2 3 4 5