string(新手练习)含代码、注释

第一次编译大概需要7秒

自我感觉重要的几点:
1.getline(cin, s)的赋值
2.字符串数组的使用
5.大小、判空
6.末尾添加
11.string查找
#include 
#include 
#include 
#include 
using namespace std;

int len = 4;
char ch = 'Y';

int main()
{
    ///1.赋值
    ///cin遇空格、tab、回车结束
//    string s1;
//    cin >> s1;
//    cout << s1 << '\n';
    ///吃“回车”,相当于getchar(),避免影响后续输入
//    cin.get();
    ///一行字符串的输入
//    string s2;
//    getline(cin, s2);
//    cout << s2 << '\n';



///2.字符串数组(竟然可以访问每个string里的每个字符!!!)
    string a[10];
    for(int i = 0; i < 5; ++i)
    {
        getline(cin, a[i]);
        if(i != 4)
            cin.get();
    }
    for(int i = 0; i < 5; ++i)
    {
        cout << a[i] << '\n';
    }
    ///访问一个string里的字符
	string s[5];
    s[0] = "ABCDE";
    for(int i = 0; i < 5; ++i)
    {
        cout << s[0][i] << '\n';
    }


    ///3.初始化
    cout << "1.初始化练习" << '\n';
    cout << "(1).直接等于号赋值" << '\n';
    string s3 = "嘤嘤嘤";
    cout << s3 << '\n';
    ///复制粘贴
    string s8;
    cout << "(2).字符串copy赋值" << '\n';
    s8 = s3;
    cout << s8 << '\n';
    ///用指向已知字符串的字符指针 初始化 字符串
    cout << "(3).指向已知字符串的字符指针 初始化" << '\n';
    char * s = "好傻好没用的做法";
    string s4(s);
    cout << s4 << '\n';
    cout << "(4).assign分配赋值——字符指针版" << '\n';
    s8.assign(s);
    cout << s8 << '\n';
    ///一个汉字占两个字节从1开始
    cout << "(5).assign分配前10个字符(字节)赋值——字符指针版" << '\n';
    s8.assign(s, 10);
    cout << s8 << '\n';
    ///另类复制粘贴
    cout << "(6).assign分配赋值——字符串版——相当于等于号copy" << '\n';
    s8.assign(s4);
    cout << s8 << '\n';
    ///用n个字符 初始化 一个字符串
    cout << "(8).用len个ch字符初始化" << '\n';
    string s5(6, ch);
    cout << s5 << '\n';
    ///用len个ch字符赋值
    cout << "(9).运用assign,用len个ch字符赋值" << '\n';
    string s9;
    s9.assign(len, ch);
    cout << s9 << '\n';
    ///另类赋值
    string s10 = "ABCDEFG";
    string s11;
    cout << "(10).用assign三维截取赋值" << '\n';
    s11.assign(s10, 0, 4);
    cout << s11 << '\n';
    ///迭代器赋值
    cout << "(11).用迭代器赋值" << '\n';
    s11.assign(s10.begin(), s10.end());
    cout << s11 << '\n';



    ///4.字符操作
    ///访问指定位置的字符(感觉很耗时!!!)
//    string s6 = "qaq";
//    char b = s6[1];///输出a(字符串从0开始)
//    cout << b << '\n';




    ///5.特性描述
    string s7 = "abcdefg";
    ///当前容量 15 ???
    cout << s7.capacity() << '\n';
    ///string 对象可存放的最大字符串长度(2147483647 = 2 ^ 31 - 1)
    cout << s7.max_size() << '\n';
    ///字符串大小
    cout << s7.length() << '\n';
    ///判空
    cout << s7.empty() << '\n';
    ///重定大小,不足去补
    s7.resize(10, 'A');
    cout << s7 << '\n';
    s7.resize(5, 'B');
    cout << s7 << '\n';



    ///6.string连接
    ///算数相加
    string s12 = "HIJKLMN ";
    string s13 = "OPQ ";
    s12 += s13;
    cout << s12 << '\n';
    ///指向字符串的字符指针截取部分 添于末尾
    s13.append(s, 10);
    cout << s13 << '\n';
    s13.append(s);
    ///迭代器添加
    string s14 = "s14 ";
    cout << s14 << '\n';
    s14.append(s7.begin(), s7.end());
    cout << s14 << '\n';
    ///尾部添字符
    string s15 = "s15 ";
    s15.push_back('A');
    cout << s15 << '\n';



    ///7.比较大小
    string s16 = "abcdefg";
    string s17 = "bcdefg";
    cout << (s16 == s17) << '\n';
    cout << (s16 != s17) << '\n';
    cout << (s16 > s17) << '\n';
    cout << (s16 >= s17) << '\n';
    cout << (s16 < s17) << '\n';
    cout << (s16 <= s17) << '\n';
    ///大于返回1,小于返回-1,等于返回0
    cout << s16.compare(s17) << '\n';
    cout << s17.compare(s16) << '\n';
    ///拿出从s16(从0开始)的第1个字符开始连续6个字符与s17比较
    cout << s16.compare(1, 6, s17) << '\n';
    ///类似的
    cout << s16.compare(3, 5, s17, 2, 5) << '\n';



    ///8.string子串
    string s18 = "OPQRST";
    string s19 = s18.substr(3, 3);///从下表3开始的连续3个字符
    cout << s19 << '\n';



    ///9.string交换
    cout << "s18: " << s18 << '\n' << "s19: " << s19 << '\n';
    s18.swap(s19);
    cout << "s18: " << s18 << '\n' << "s19: " << s19 << '\n';



    ///10.字符串的流处理,头文件
    string str24("hello,this is a test");
    istringstream is(str24);
    string s20, s21, s22, s23;
    is >> s20 >> s21 >> s22 >> s23;
    ///s20="hello,this",s21="is",s22="a",s23="test"
    ostringstream os;
    os << s20 << s21 << s22 <

你可能感兴趣的:(所谓函数)