1. 程式人生 > >C++ 字串 6-- 18.18~19.string型字串的拷貝

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