一 点睛
函数原型:
size_t copy (char* s, size_t len, size_t pos = 0) const;
函数功能:
copy函数的作用是从string对象中取出若干字符存放到数组s中。其中,s是字符数组,n表示要取出字符的个数,pos表示要取出字符的开始位置。
函数参数:
-
参数s:字符数组,用来存放从string对象取出的字符。
-
参数len:取出的字符个数。
-
参数pos:要取出的字符在string对象中的开始位置。
函数的返回值:
函数返回取出字符的个数。
二 实战
#include<iostream>
#include<string>
using namespace std;
int main (){
size_t length;
char buffer[8];
string str("Test string......");
cout<<"str:"<<str<<endl;
length=str.copy(buffer,7,5);
buffer[length]='\0';
cout<<"str.copy(buffer,7,5),buffer contains: "<<buffer<<endl;
length=str.copy(buffer,str.size(),5);
buffer[length]='\0';
cout<<"str.copy(buffer,str.size