当前位置:首页C++程序设计 > 正文

C++中 string类定义、相关函数引用

作者:野牛程序员:2023-02-24 15:00:39C++程序设计阅读 2813

在C++中,可以使用string类来处理字符串,它提供了一系列方便的字符串操作函数。下面介绍string类的定义和一些常用的成员函数。

  1. string类的定义

string类是C++标准库中的一个类,它定义在头文件<string>中。可以使用以下语句来定义一个string对象:

#include <string>
using namespace std;

string str = "hello";

在上面的代码中,我们首先包含了<string>头文件,并使用using namespace std来使用std命名空间。然后定义了一个string对象str,并初始化为"hello"。

  1. 常用的成员函数

string类提供了许多成员函数来处理字符串,下面列举一些常用的函数:

  • length():返回字符串的长度。

  • size():返回字符串的长度,与length()函数功能相同。

  • empty():判断字符串是否为空。

  • clear():清空字符串中的所有字符。

  • append():在字符串的末尾添加一个字符串或字符。

  • substr():返回字符串中指定位置和长度的子串。

  • find():在字符串中查找指定的子串,返回其位置。如果没有找到,则返回string::npos。

  • replace():将字符串中指定的子串替换为另一个字符串或字符。

  • erase():删除字符串中指定位置和长度的字符。

下面是一个使用上述成员函数的示例程序:

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

int main()
{
    string str = "hello, world";
    cout << "原始字符串:" << str << endl;

    // 使用length和size函数获取字符串的长度
    cout << "字符串的长度为:" << str.length() << endl;
    cout << "字符串的长度为:" << str.size() << endl;

    // 使用empty函数判断字符串是否为空
    if (str.empty()) {
        cout << "字符串为空。" << endl;
    } else {
        cout << "字符串不为空。" << endl;
    }

    // 使用append函数在字符串的末尾添加一个字符串或字符
    str.append("!");
    cout << "添加'!'后的字符串:" << str << endl;

    // 使用substr函数获取字符串的子串
    string sub = str.substr(0, 5);
    cout << "子串为:" << sub << endl;

    // 使用find函数在字符串中查找指定的子串
    int pos = str.find("world");
    if (pos != string::npos) {
        cout << "找到了\\"world\\",位置为:" << pos << endl;
    } else {
        cout << "没有找到\\"world\\"。" << endl;
    }

    // 使用replace函数将字符串中的子串替换为另一个字符串
    str.replace(pos, 5, "C++");
    cout << "替换后的字符串:" << str << endl;

    // 使用erase函数删除字符串中的一部分字符
    str.erase(0, 6);
    cout << "删除部分字符后的字符串:" << str << endl;

    // 使用clear函数清空字符串
    str.clear();
    cout << "清空后的字符串:" << str << endl;

    return 0;
}

在上面的示例程序中,我们首先定义了一个string对象str,并初始化为"hello, world"。然后使用length和size函数获取字符串的长度,并使用empty函数判断字符串是否为空。接着使用append函数在字符串的末尾添加一个字符"!",并使用substr函数获取字符串的子串。使用find函数在字符串中查找指定的子串"world",如果找到了则输出其位置。然后使用replace函数将字符串中的子串"world"替换为"C++",使用erase函数删除字符串中的前6个字符,最后使用clear函数清空字符串。运行程序后,输出结果如下:

原始字符串:hello, world
字符串的长度为:12
字符串的长度为:12
字符串不为空。
添加'!'后的字符串:hello, world!
子串为:hello
找到了"world",位置为:7
替换后的字符串:hello, C++!
删除部分字符后的字符串:C++!
清空后的字符串:

可以看到,程序使用了string类的许多成员函数,对字符串进行了各种操作。

除了上述例程中使用的成员函数外,string类还有许多其他有用的成员函数,下面列举一些常用的:

  1. at:获取指定位置的字符。

string str = "hello";
char c = str.at(0); //获取第一个字符'h'
  1. front和back:获取字符串的第一个字符和最后一个字符。

string str = "hello";
char first = str.front(); //获取第一个字符'h'
char last = str.back(); //获取最后一个字符'o'
  1. compare:比较两个字符串的大小。

string str1 = "hello";
string str2 = "world";
int result = str1.compare(str2); //返回负数,表示str1小于str2
  1. c_str:将字符串转换为C风格字符串。

string str = "hello";
const char* cstr = str.c_str(); //将str转换为C风格字符串
  1. getline:从输入流中读取一行字符串。

string line;
getline(cin, line); //从标准输入读取一行字符串
  1. stoi、stol、stof、stod:将字符串转换为数值类型。

string str = "123";
int num = stoi(str); //将str转换为整型数值

to_string:将数值类型转换为字符串

int num = 123;
string str = to_string(num); //将num转换为字符串

这些成员函数覆盖了字符串的常见操作,开发者可以根据需要选择使用。同时,string类还支持重载运算符,可以使用加号进行字符串的拼接操作,如:

string str1 = "hello";
string str2 = "world";
string str3 = str1 + ", " + str2; //拼接两个字符串

除了string类本身提供的成员函数和运算符,C++标准库还提供了一些与string相关的算法,可以对字符串进行各种操作。下面列举一些常用的算法:

  1. find、rfind、find_first_of、find_last_of:在字符串中查找指定字符或子串的位置。

string str = "hello, world";
int pos = find(str.begin(), str.end(), ',') - str.begin(); //查找','的位置
  1. copy:将字符串中的字符复制到指定位置。

string str = "hello";
char buf[10];
copy(str.begin(), str.end(), buf); //将str中的字符复制到buf中
  1. transform:将字符串中的字符进行变换。

string str = "hello";
transform(str.begin(), str.end(), str.begin(), ::toupper); //将str中的字符全部转换为大写
  1. reverse:将字符串反转。

string str = "hello";
reverse(str.begin(), str.end()); //将str反转
  1. sort:将字符串中的字符进行排序。

string str = "hello";
sort(str.begin(), str.end()); //将str中的字符进行排序

这些算法可以通过包含头文件<algorithm>来使用。它们通常与迭代器结合使用,可以对字符串中的字符进行各种操作,方便高效。

野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击