C++ 字符串 6-- 18.18~19.string型字符串的拷贝

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
     18-18 18.18~19.string型字符串的拷贝
---------------------------------*/
int main()
{
cout<<"------memmove------------:"<<endl;
char ch1[50]="1234567890"; 
char ch2[]="abcde";
cout<<ch1<<endl<<ch2<<endl;
memmove(ch1,ch2,5); //如果拷贝ch2的6个字符,则结束标志将被拷贝到ch1
cout<<ch1<<endl<<ch2<<endl;


cout<<"-------string copy()-----------:"<<endl;
string str1="1234567890"; 
strcpy(ch2,"abcdefg");
cout<<str1<<endl<<ch2<<endl;
int n=str1.copy(ch2,3,0); //将str1脚标为0的字符开始,连续拷贝3个字符到ch2中
cout<<str1<<endl<<ch2<<endl;
cout<<"str.copy()拷贝了="<<n<<"个字符"<<endl;


return 0;

}

运行结果:

------memmove------------:
1234567890
abcde
abcde67890
abcde
-------string copy()-----------:
1234567890
abcdefg
1234567890
123defg
str.copy()拷贝了=3个字符
Press any key to continue

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80521586