从string类型变量或const char * 中读取部分字符串到string变量中:
赋值(assign)
语法:
basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );
函数以下列方式赋值:
用str为字符串赋值,
用str的开始num个字符为字符串赋值,
用str的子串为字符串赋值,子串以index索引开始,长度为len
用num个字符ch为字符串赋值.
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hello,world";
string m;
m.assign(s,5,3);
cout<<m<<endl; //输出为 ,wo
const char*num=""qazwsx,ed;
string n;
n.assign(&num[0],3);
cout<<num<<endl; //输出为qazwsx,ed
cout<<n<<endl; //输出位qaz
return 0;
}