1. std::strcpy
功能:将一个字符串复制到另一个字符串(如果字符串重叠,该行为是未定义);
定义于头文件 <cstring>
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 <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[]= "Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;
return 0;
}
Output:
str1: Sample string
str2: Sample string
str3: copy successful
2.std::strncpy
定义于头文件<cstring>
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 &