如何给数组用fill函数和memset函数给数组赋初值

fill是按照单元来赋值的,所以可以填充一个区间的任意值

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
#define INF 1e9+7
int main()
{
    int a[100];
    fill(a,a+10,100);
    for(int i=0;i<10;i++)
        printf("%d ",a[i]);
    return 0;
}

 

memset函数一般只用于赋初值为0或者-1

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
#define INF 1e9+7
int main()
{
    int a[100];
    memset(a,-1,sizeof(a));
    for(int i=0;i<10;i++)
        printf("%d ",a[i]);
    return 0;
}

  

posted @ 2018-07-10 10:30  执||念  阅读(1213)  评论(0编辑  收藏  举报