字符串基本操作

/*
-------------------------------------------------
   Author:       wry
   date:         2022/2/24 19:18
   Description:  String
-------------------------------------------------
*/

#include <bits/stdc++.h>

using namespace std;

struct Text{
    int data;
    char ch;
    Text(int d,char c):data(d),ch(c){};
};

int main() {
    //构建
    string s0 = "this is a string!";
    string s1(s0);    //复制整个s0至s1
    string s2(s0,3,10);    //从s0的下标为3的位置开始的10个值
    string s3(s0,6);      //从s0下标为6开始的后面所有值
    string s4(10,'a');    //复制10次'a',注意只能是单字符
    cout << s0 << endl << s1 << endl << s2 << endl << s3 << endl << s4 << endl;

    //插入
    string s5 = "to be question";
    string s6 = "that is a ";
    string s7 = "or not world";
    string s8;
    s8.insert(0,s5);                //to be question
    s8.insert(6,s7,0,7);    //to be or not question
    s8.insert(13,"to be ");           //to be or not to be question
    s8.insert(19,s6);               //to be or not to be that is a question
    cout << s8 << endl;

    //删除
    s8.erase(22);    //删除下标为22及其以后
    cout << s8 << endl;
    s8.erase(0,9);   //删除下标为0开始的9个值
    cout << s8 << endl;

    //清空
    s8.clear();
    cout << s8 << endl;

    //加法运算
    string s9 = s5 + ",";
    cout << s9 << endl;
    string s10 = s9 + s6 + "," + s7;
    cout << s10 << endl;

    //逻辑运算
    string str1 = "ab";
    string str2 = "Ab";
    string str3 = "bc";
    string str4 = "abb";
    string str5 = "d";
    cout << (str1 >= str2) << endl;    //小写字符ASCII更大
    cout << (str1 <= str3) << endl;    //字母越大ASCII越大
    cout << (str1 <= str4) << endl;    //前面相同,字符串越长,则越大
    cout << (str1 <= str5) << endl;    //字符串先比较对应大小,出现某个字符更大则不会比较后面,更不会看谁更长
    //字符串(char[])的比较用strcmp(a,b)。
    //如果等于0,表示一样大;如果>0,表示a大;如果<0,表示b大。
    char a[100] = "abcd";
    char b[100] = "bcd";
    cout << strcmp(a,b) << endl;


    //常用函数
    string st1 = "Hello World, End World";
    //大小size()
    cout << st1.size() << endl;    //输出22,是此字符串的真是长度

    //位置find(),可以处理字符串或者字符
    cout << st1.find("World") << endl;   //找到第一个World的起始位置(下标)
    cout << st1.find("l",10) << endl;    //找到下标为10开始的第一个字符为l的位置(下标)
    cout << st1.find("a") << endl;    //找不到返回-1

    //子串substr()
    string st2 = st1.substr(13);    //从下标为13开始的所有
    string st3 = st1.substr(13,3);    //从下标为13开始的3个元素的子串
    cout << st2 << endl << st3 << endl;
    
    //字符串的输入与输出
    string str;
    cin >> str;      //cin输入不能包含空格
    getline(cin,str);   //可包括空格,但不包括回车;先清空缓冲区str(类似clear()),再输入一整行数据,
    cout << str << endl;
    for (int i=0;i<str.size();i++) {
        printf ("%c ",str[i]);   //对string类型而言,每个位置都是一个char类型的单字符
    }
    char ch[100];
    gets(ch);   //可以输入空格
    puts(ch);

    return 0;
}

注意:在输入时会用到while循环,各种输入方式又很多不同

  1. while (cin>>str)      不能输入空格
  2. while (getline(cin,str))      能输入空格
  3. while (gets(ch)!=NULL)   能输入空格
  4. while (scanf("%s",ch)!=EOF)    不能输入空格
  5. while (scnaf ("%d%d%d",&n1&n2,&n3)!=EOF)      数字输入
posted @ 2022-02-24 20:42  火星架构师  阅读(146)  评论(0)    收藏  举报