std::strcpy、strncpy、memset、memcpy用法

1. std::strcpy

功能:将一个字符串复制到另一个字符串(如果字符串重叠,该行为是未定义);
定义于头文件

 char *strcpy( char *dest, const char *src );

参数:
destination

Pointer to the destination array where the content is to be copied.

source

C string to be copied.

Return Value

destination is returned.

dest :指向复制内容存放的首地址
src :需要被复制的C风格的字符串
返回值 :复制后的字符串的首地址

Example:

/* strcpy example */
#include 
 #include 
 using namespace std;
  int main ()
  {
    char str1[]= "Sample string";
    char str2[40];
    char str3[40];
   strcpy (str2,str1);
   strcpy (str3,"copy successful");

   cout<<"str1:"<cout<<"str2:"<cout<<"str3:"<return 0;
 }

Output:

str1: Sample string
str2: Sample string
str3: copy successful

2.std::strncpy

定义于头文件

char *strncpy( char *dest, const char *src, std::size_t count );

功能:将一个字符串的一部分复制到另一个字符串;
说明:从原地址source开始,复制num个字符到dest开始的地址;
destination

Pointer to the destination array where the content is to be copied.

source

C string to be copied.

num
Maximum number of characters to be copied from source.
size_t is an unsigned integral type.

说明:从源地址source开始,复制num个字符到dest开始的地址

Example

C版:

    /* strncpy example */
#include 
#include 

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy ( str2, str1, sizeof(str2) );

  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';  ** /* null character manually added */**

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}

Output:

To be or not to be
To be or not to be
To be

C++:

#include 
#include 

int main()
{
    const char* src = "hi";
    char dest[6] = {'a', 'b', 'c', 'd', 'e', 'f'};;
    std::strncpy(dest, src, 5);
 //多出的用空字符填充
    std::cout << "The contents of dest are: ";
    for (char c : dest) {
        if (c) {
            std::cout << c << ' ';
        } else {
            std::cout << "\\0" << ' ';
        }
    }
    std::cout << '\n';
    return 0;
}

output:
The contents of dest are: h i \0 \0 \0 f

3.std::memcpy

定义于头文件

void * memcpy ( void * destination, const void * source, size_t num );

功能:将一个缓冲区复制到另一个缓冲区;

Example

#include 
#include 

int main()
{
    char source[] = "once upon a midnight dreary...";
    char dest[4];
    std::memcpy(dest, source, sizeof dest);
    for (char c : dest) {
        std::cout << c << '\n';
    }
    return 0;
} 

output:
o
n
c
e

4.std::memset

定义于头文件

void* memset( void* dest, int ch, std::size_t count );

Fill block of memory

Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

功能:将指针ptr所指向的内存空间开始的num个内存单元设置成value的值;

参数:
ptr

Pointer to the block of memory to fill.

value

Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.

num

Number of bytes to be set to the value.
size_t is an unsigned integral type.

C:

/* memset example */
#include 
#include 

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

Output:

—— every programmer should know memset!

C++版:

#include 
#include 

int main()
{
    int a[20];
    std::memset(a, 0, sizeof(a));
    std::cout << "a[0] = " << a[0] << '\n';
    return 0;
}

output:
a[0] = 0

附:整个用法代码全

/*Jason Gel        2016-12-9   编辑器:vs2013
*博客地址:http://write.blog.csdn.net/postlist/0/0/enabled/2
*说明:std:strcpy、strncpy、memcpy、memset的用法,将相应的子函数变成main函数即可。
*/
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
using namespace std;

int main_strcpy()
{
    char str1[] = "Sample string";
    char str2[40];
    char str3[40];
    strcpy(str2, str1);                 // str2 <-----str1
    strcpy(str3, "copy successful");    //  str3<-----const char *

    cout << "str1:" << str1 << endl;
    cout << "str2:" << str2 << endl;
    cout << "str3:" << str3 << endl;

    system("pause");
    return 0;
}

int main_strncpy()
{
    const char* src = "hi";
    char dest[6] = { 'a', 'b', 'c', 'd', 'e', 'f' };;
    std::strncpy(dest, src, 5);    // dest <----(5个char)---src
    cout << dest[0] << endl;    
    cout << dest[1] << endl;

    if (dest[2] == 0 && dest[2] =='\0')
        cout << "des[2]为空字符" << endl;
    //多出的用空字符填充
    std::cout << "The contents of dest are: ";
    for (char c : dest) {       //范围for循环,同 for( auto c: dest )
        if (c) {
            std::cout << c << ' ';
        }
        else {
            std::cout << "\\0" << ' ';
        }
    }
    std::cout << '\n';
    system("pause");
    return 0;
}
int main_memcpy()
{
    char source[] = "once upon a midnight dreary...";
    char dest[4];
    std::memcpy(dest, source, sizeof dest);  //dest<---(4个char)--source
    for (char c : dest) {
        std::cout << c << '\n';
    }
    system("pause");
    return 0;
}

int main()
{
    int a[20];
    std::memset(a, 0, sizeof(a));         //a<----(20个int)--0
    std::cout << "a[10] = " << a[10] << '\n';
    system("pause");
    return 0;
}

你可能感兴趣的:(C++,C/C++笔记,C语言)