string类 1

//string类
// string 类 是一个模板类, 它的定义如下:
// typedef basic_string string;
#include 
#include 
using namespace std;
int main()
{
    string s1("hello");//一个参数的构造函数
    string s2(8, '6');//两个参数的构造函数 int char?
    string month = "May";//赋值操作符初始化 
    //不提供以字符和整数为参数的构造函数
//错误的初始化方法:
/*    string error1 = 'c';
    string error2('c');
    string error3 = 1;
    string error(1);*/
    //char可以赋值给string对象 
    string s3;
    s3 = 'c';
    //构造的string太长而无法表达 ? 会抛出length_error异常

    //string 对象的长度用成员函数 length()读取;
    string s4("hhhhhh");
    cout  s5;
    //string 支持getline函数
    string s6;
    getline(cin, s6);
    //string的赋值和连接
    string s7("12345"), s8;
    s8 = s7;//用 ‘=’ 赋值
    s8.assign(s7);
    s8.assign(s7, 1, 3);//从s7中下标为1开始复制3个字符给s8
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论