String类的构造方法

String类的构造方法

C++中,模板类string的构造方法
直接上代码…

  1 #include<iostream>
  2 using namespace std;
  3 
  4 
  5 int main()
  6 {
  7     //模板类string的构造方法
  8 
  9     //1.没有参数的构造方法
 10     string str1;
 11     str1 = "超人";
 12     cout << "str1 = " << str1 << endl;
 13 
 14     //2.string(const char* s)
 15     const char s[] = "hello world";
 16     string str2_1(s);
 17     cout << "str2_1 = " << str2_1 << endl;
 18     string str2_2("世界你好.");
 19     cout<< "str2_2 = " << str2_2 << endl;
 20 
 21     //3.string(size_type n, char c)  将n个c连成字符串
 22     string str3(12, 'd');
 23     cout << "str3 = " << str3 << endl;
 24 
 25     //4.string(const string *str)
 26     string str4(str2_1);
 27     cout << "str4 = " << str4 << endl;
 28 
 29     //5.string(const char* s, size_type n) 截取s指向的字符串的前n个字符
 30     char greeting[] = "hello bill";
 31     string str5(greeting, 5);
 32     cout << "str5 = " << str5 << endl;
 33 
 34     //6.template<class Iter>string(Iter begin, Iter end)  截取begin和end之间的字符,不包括end
 35     string str6(greeting + 6, greeting +11);
 36     string str6_1(&str2_1[0] + 1, &str2_1[0] + 5);
 37 
 38     cout << "str6 = " << str6 << endl;
 39     cout << "str6_1 = " << str6_1 << endl;
 40 
 41     //7.string(const string& str, size_type pos, size_ype npos)
 42     string str7_1(str2_1, 6);
 43     cout << "str7_1 = " << str7_1 << endl;
 44     string str7_2(str2_1, 6, 2);
 45     cout << "str7_2 = " << str7_2 << endl;
 46 
 47     //8.(c++ 11)string(initialier+list<char> list)
 48     string str8_1 = {'H', 'a', 'c'};
 49     string str8_2{'a', 'c', 'd'};
 50     cout << "str8_1 = " << str8_1 << endl;
 51     cout << "str8_2 = " << str8_2 << endl;
 52 
 53     return 0;
 54 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值