编程日志5.27
string基础概念
算法:
#include<iostream>
 //#include<string> io已经包含这个
 using namespace std;
int main() {
     char a[100] = "英雄哪里出来";
     cout << a << endl;//字符串
     //cout <<(void *) a << endl;//指针
     string b= "英雄哪里出来";//不用关心字符串多长 自动扩容
     cout << b << endl;
     return 0;
 }
运行结果:
英雄哪里出来
 英雄哪里出来
string对象创建
算法:
#include<iostream>
 using namespace std;
int main() {
     //1.无参构造
     string s1;
     cout << "1:";
     cout << s1 << endl;
    //2.初始化列表
     string s2({ 'h','e','l','l','o' });
     cout << "2:";
     cout << s2 << endl;
    //3.字符串的初始化
     string s3("英雄哪里出来");
     cout << "3:";
     cout << s3 << endl;
     
     //4.字符串的前n个字符
     string s4("英雄哪里出来", 6);//一个中文字符占两个字符
     cout << "4:";
     cout << s4 << endl;
    string s4_1("英雄哪里出来", 5);//一个中文字符站两个字符 奇数显示n/2-1个 最后一个字的第一个字符不显示
     cout << "4_1:";
     cout << s4_1.size() << endl;
     cout << (int)s4_1[4] << endl;
     cout << s4_1 << endl;
    //5.拷贝构造函数
     string s5(s4);
     cout << "5:";
     cout << s5 << endl;
    //6.a个字符b
     string s6(8, 'o');
     cout << "6:";
     cout << s6 << endl;
    return 0;
 }
运行结果:
1:
 2:hello
 3:英雄哪里出来
 4:英雄哪
 4_1:5
 -60
 英雄
 5:英雄哪
 6:oooooooo
