C++ memset()详细说明

C++ memset()详细说明

C++编程语言中,memset()是一个用于填充内存块的函数。

  • 首先,它将’ch’的值转换为无符号字符。这里的’ch’是指在memset()函数中传递的要被填充为另一个值的字符。
  • 然后,它将字符’ch’复制到指针st[]所指向的对象的前n个字符中。
  • 这里的’n’是指块的大小,它在memset()中被指定,并且它必须等于或小于指针st[]所指向的对象的大小。
  • 如果n的值大于指针st[]所指向的对象的大小,它将会产生错误,从而导致未定义的行为。
  • 如果在某些情况下出现了一个不能被复制的对象,那么它也会产生错误,并且函数的行为与前面的情况相同,即未定义。
  • C++编程语言中,memset()函数位于头文件中;如果没有包含这个头文件,你将无法使用memset()函数。
  • 这里,不能被复制的对象包括:数组、C兼容的结构体、标量等;因此,在这种情况下,memset()函数的行为是未定义的。
  • memset()函数和memcpy()函数之间唯一的区别是,memset()函数不仅复制值,还将其替换为其他替代值。示例,如果我们想将一个特定字符串(如’cool’)的每个字符替换为字母’f’,结果将是’ffff’。但是,在memcpy()函数中,它只是从一个地方复制值到另一个地方,或者从一个特定位置复制一块内容并放置到另一块内容中。

memset()函数的语法

void* memset ( void* st , int ch , size_t n ) ;

现在,让我们讨论这里使用的术语。

  1. st [ ]:st [ ]表示要填充的内存的指针。它是指向对象的指针,其中已复制字符 ‘ch’。根据程序员的选择,他们可以根据需要和工作将名称从st更改为其他名称。
  2. ch:’ch’在这里指的是需要填充的字符。它也因程序员而异,他们可以相应地重命名它。它在需要时转换为unsigned char,并将其转换为int格式,但int可接受的值仅为0和-1。
  3. n:它表示块的大小,或者可以说是指定字符 ‘ch’ 需要复制和转换为memset()函数中传递的值的次数的值。

C++的memset()函数的优点:

  1. 提高可读性:C++的memset()函数主要用于将整个字符串的每个字符转换为作为memset()函数的输入传递的特定int值。它是一行代码,非常简短,从而增加了可读性。
  2. 减少代码行数:与使用循环不必要地给每个字符分配和转换在该memset()函数中仅作为输入传递的int值的值相比,使用memset()函数可以轻松完成相同的任务,而不需要使用循环。
  3. 更快:使用memset()函数将给定字符串的每个字符转换为通过输入传递的值,可以更快地完成,无论是int类型还是其他类型,这取决于程序员。它的工作速度非常快,而不需要应用循环和while语句来执行相同的任务。
  4. 有助于解决对齐问题:C++的memset()函数帮助程序员解决对齐问题。有时,会出现处理器中数据错位的情况,导致程序出错。在这种情况下,C++的memset()和memcpy()函数是解决该问题的终极解决方案。

C++中memset()的实现: 如上所述,memset()函数的概念及其工作原理,让我们通过一个示例更详细地了解它。

示例1

// Program to implement memset ( ) function in C++

#include 
#include              // header file that contains the memset ( ) function, without it            // we cannot able to access the use of memset ( ) function
using namespace std ;

int main()
{
    char st [ ] = " JavaTpoint " ;
    char ch ;
    cout << " Printing the given string : " << st << endl ;
    cout << " Enter an alphabet from which you want to replace the each charater of the given string : " ;
    cin >> ch ;
    memset ( st , ch , sizeof ( st ) ) ;
    cout << " Printing the string after replacing the each character of the string with the given substitute : " << endl ;
    cout << " " << st ;
    return 0 ;
}

以上程序的输出:

C++ memset()详细说明

在上面的例子中,我们看到字符串’JavaTpoint’的每个字符都被转换为用户传递的单个字母。这里空格也被视为一个单独的字符。

示例2

// Program to implement memset ( ) function in C++

#include < iostream >
#include < cstring >             // header file that contains the memset ( ) function, without it //we cannot able to access the use of memset ( ) function 
using namespace std ;
int main()
{
    int arr [100] ;
    int n , i , p ;
    cout << " Enter size of an integer array: " ; 
    cin >> n ;
    cout << " Enter an array elements: " << endl ;
    for ( i = 0 ; i < n ; i++ )
    {
        cin >> arr [ i ] ;
    }
    cout << " Printing the given array : " << endl ;
    for ( i = 0 ; i < n ; i++ )
    {
        cout << " " << arr [ i ] << endl ;
    }
    cout << " Enter an integer value from which you want to replace the each charater of the given string : " ;
    cin >> p ;
    memset ( arr , p , sizeof( arr ) ) ;
    cout << " Printing the array after replacing the each value of the array with the given substitute : " << endl ;
    for ( i = 0 ; i < n ; i++ )
    {
        cout << " " << arr [ i ] << endl ;
    }

    return 0 ;
}

以上程序的输出结果:

C++ memset()详细说明

在上面的例子中,我们看到了用户输入的字符串“JavaTpoints”中的每个字符是如何被转换为整数值。在这里,空格也被视为单个字符。在这里,只有0和-1的整数值被考虑,这意味着字符串的每个字符只被转换为0和-1,而不是使用其他整数值,将导致一个垃圾值,这意味着如果您输入了2,则字符串的所有字符都被替换为垃圾值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程