"C + +" Fill function, the difference between fill and memset function

Source: Internet
Author: User

  • memset function
    • Fill a character by byte
    • Inside the header file. <cstring>
  • Fill function
    • Assigns the element of an interval to the same value according to the unit assignment value
    • Inside the header file. <algorithm>
  • Because the Memset function is populated by bytes, the general memset can only be used to populate the char array (since only char is a byte) if the int array is filled, except for 0 and-1, the others cannot. Because only 00000000 = 0,-1 Similarly, if we fill each bit with "1", it will result in filling into "11111111"
  • The fill function can be assigned any value, and the method of use is particularly simple:
    • Fill (arr, arr + N, content to be filled in);
    • For example:
    • #include <cstdio><algorithm>usingnamespace  std; int Main () {    int arr[];     Ten 2 );     return 0 ;}
    • Vectors can also:
    • #include <algorithm>#include<vector>#include<iostream>using namespacestd;intmain () {vector<int> v{0,1,2,3,4,5,6,7,8,9}; Fill (V.begin (), V.end (),-1); return 0;}
    • The way to use memset is:
    • #include <iostream><cstring>usingnamespace  std; int Main () {    int a[];     0 sizeof a);     return 0 ;}

"C + +" Fill function, the difference between fill and memset function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.