C++_22_string类型 - 重写版

发布时间 2023-12-04 00:10:59作者: 尘落曦枫

string类型·变量定义

  C++ 中提供了一个 string 内建数据类型,它可以替代 C 语言中的 char* 数组。
  使用 string 数据类型时,需要在程序中包含头文件<string>

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1;                 //定义s1未能初始化,默认赋值s1为" "(空字符串);
    string s2 = "string";    //定义s2初始化赋值为字符串"string",但是结尾没有'\0'
    string s3 = s2;        //定义s3直接用s2初始化,s3 的内容也是 "string";
    string s4 (10, 's');       //定义s4初始化为10个's'字符组成的字符串,即 "ssssssssss"。
    return 0;
}
//string类型定义的变量可以通过3种方式赋值:“=”赋值操作符、string类型变量或者字符串赋值、括号赋值

  string 类型的本质是一个 string 类,而我们定义的变量则是一个个的 string 类的对象
    
因此可以出现s1.c_str() 用法

 

string s = "string";
int len = s.length();

  string类型为我们提供length函数来返回得到变量的字符串长度
  且因为string类型字符串后没有'\0',所以返回值就是实际字符串长度

 

转化为char数组字符串

  C++中可以使用string类型来代替C语言中的char*字符串,但是为便于实际编程中灵活运用;
  系统为我们提供一个转化函数 c_str();将string类型变量转化为一个const字符串数组指针char*类型)

#include<iostream>
#include<string>
#include <string.h>
using namespace std;

int main(int argc, char const *argv[])
{
    string filename = "hello world";
    string newname = filename.c_str();
    cout << filename << endl;
    cout << newname << endl;
    cout << "length(filename) = " << filename.length() << endl;
    cout << "length(newname) = " << newname.length() << endl;
    return 0;
}
输出:
zl@LAPTOP-67MRI3LV:/mnt/d/VsCode$ ./1
hello world
hello world
length(filename) = 11
length(newname) = 11

 

string类型变量的输入和输出

  string类型变量的输入输出和其他类型变量一样

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s;
    cin>>s;
    cout<<s<<endl;
    return 0;
}

 

string类型字符串的连接

  对于 string 类型变量,我们可以直接用“+”或者“+=”进行字符串的连接。
    用“+”风格字符串进行字符串连接时,操作符左右两边一边是 string 字符串,另一边可以是一个 string 字符串和一个 C 风格的字符串或者一个 char 字符。
    用“+=”风格字符串进行字符串连接时,操作符右边既可以是一个 string 字符串,也可以是一个 C 风格字符串或一个 char 字符。

 

修改字符串

  string类型字符和字符串数组(char *)类似,string 字符串也可以按照下标逐一访问每一个字符,string 字符串的起始下标仍是从 0 开始。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 ;
    s1 = "1234567890";
    for(int i=0; i<s1.length(); i++)
        cout<<s1[i]<<" ";
    cout<<endl;
    s1[5] = '5';
    cout<<s1<<endl;
    return 0;
}

  操作string类型变量函数
    string erase()函数 ------- 删除子字符串
      erase() 函数有两个参数:第一个参数是要删除的子字符串的起始下标;
                   第二个参数是要删除子字符串的长度;
      如果第二个参数不指明,则从第一个参数获取起始下标,然后一直删除至字符串结束。
      待删除字符串最多删除至字符串结尾

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2, s3;
    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    cout<< s3 <<endl;
    return 0;
}

运行:
1234567890
12345
1234590

    string insert()函数 ------- 插入子字符串
      insert() 函数同样有两个参数:第一个参数表示插入位置,
                    第二参数表示要插入的字符串,
      第二个参数既可以是 string 变量,又可以是 C 风格的字符串。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.insert(5, s3);
    cout<< s1 <<endl;
    s2.insert(5, "aaa");
    cout<< s2 <<endl;
    return 0;
}
答案:
12345aaa567890
12345aaa567890

    string replace()函数 ------- 替换子字符串
      replace() 函数有三个参数:第一个参数表示待替换的子字符串的其实下标,
                   第二个参数表示待替换子字符串的长度,
                   第三个参数表示要替换子字符串的字符串。
      第三个参数同样可以是 string 类型变量或 C 风格字符串。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.replace(5, 4, s3);
    cout<< s1 <<endl;
    s2.replace(5, 4, "aaa");
    cout<< s1 <<endl;  
    return 0;
}
答案:
12345aaa0
12345aaa0

    string swap()函数 ------- 互换两个string类型的值

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "string";
    string s2 = "aaaaaa";
    s1.swap(s2);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    return 0;
}
答案:
aaaaaa
string

 

 

 

提取子字符串

  substr() 函数可以提取 string 字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    return 0;
}
答案:
first second third
second

 

 

查找字符串

   find() 函数可以在字符串中查找子字符串中出现的位置。
    该函数有两个参数:第一个参数是待查找的子字符串;
             第二个参数是表示开始查找的位置(字符串下标)。
    如果第二个参数不指名的话则默认从 0 开始查找,也即从字符串首开始查找。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

  函数返回的是子字符串出现在字符串中的起始下标。如果没有查找到子字符串,则返回一个无穷大值。

  rfind() 函数与 find() 函数很类似,同样是在字符串中查找子字符串。
  不同的是,find() 函数是从第二个参数开始往后查找,
          rfind() 函数则是最多查找到第二个参数处,如果没有找到子字符串,则返回一个无穷大值。

 

 

字符串的比较

   ==!=<=>=<>操作符都可以用于进行 string 类型字符串的比较,这些操作符两边都可以是 string 字符串,也可以一边是 string 字符串另一边是字符串数组。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "secondsecondthird";
    string s2 = "secondthird";
    if( s1 == s2 )
        cout<< " == " <<endl;
    if( s1 != s2 )
        cout<< " != " <<endl;
    if( s1 < s2 )
        cout<< " < " <<endl;
    if( s1 > s2 )
        cout<< " > " <<endl;
    return 0;
}