zoukankan      html  css  js  c++  java
  • 【C++】fill函数,fill与memset函数的区别

    • memset函数

      • 按照字节填充某字符
      • 在头文件里面
    • fill函数

      • 按照单元赋值,将一个区间的元素都赋同一个值
      • 在头文件里面
    • 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组,(因为只有char型占一个字节)如果填充int型数组,除了0和-1,其他的不能。因为只有00000000 = 0,-1同理,如果我们把每一位都填充“1”,会导致变成填充入“11111111”

    • 而fill函数可以赋值任何,而且使用方法特别简便:

      • fill(arr, arr + n, 要填入的内容);
      • 例如:
      #include <cstdio>
      #include <algorithm>
      using namespace std;
      int main() {
          int arr[10];
          fill(arr, arr + 10, 2);
          return 0;
      }
      
      • vector也可以:

        #include <algorithm>
        #include <vector>
        #include <iostream>
        using namespace std;
        int main(){
            vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            fill(v.begin(), v.end(), -1);
         	return 0;
        }
        
    • 而memset的使用方法是:

    #include <iostream>
    #include <cstring>
    using namespace std;
    int main(){
        int a[20];
        memset(a, 0, sizeof a);
        return 0;
    }
    
  • 相关阅读:
    xamp配置多域名站点
    POJ1611-The Suspects-ACM
    POJ2524-宗教问题-并查集-ACM
    POJ3274-牛的属性-HASH-ACM
    拓扑排序-DFS
    拓扑排序
    POJ1007-DNA Sorting-ACM
    POJ1258-Agri-Net-ACM
    wdcp-apache配置错误导致进程淤积进而内存吃紧
    wdcp-apache开启KeepAlive提高响应速度
  • 原文地址:https://www.cnblogs.com/RioTian/p/12597261.html
Copyright © 2011-2022 走看看