【C++STL基础入门】深入浅出string类查找字串、返回字串和交换操作

简介: 【C++STL基础入门】深入浅出string类查找字串、返回字串和交换操作

前言


本STL使用VS2022+C++20版本

C++标准库(Standard Template Library,简称STL)是C++中非常强大和常用的一组容器、算法和函数模板,它能大大简化程序开发和提高开发效率。在STL中,string类是对字符串进行处理的重要组成部分。本文将向您介绍string类的三个基本操作:查找子串(find)、返回子串(substr)和交换操作(swap),帮助您更好地理解和应用这些功能。


一、查找字串


1、size_type find( const basic_string &str, size_type index );

此函数用于在字符串中查找给定子串 str 的第一个位置。它接收两个参数:str 是要查找的子串,index 是在哪个位置开始搜索。函数返回找到的子串的第一个字符的索引位置,如果找不到子串,则返回 string::npos。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "Hello, world!";
    std::string sub = "world";
    size_t found = str.find(sub, 0);  // 在整个字符串中搜索
    if (found != std::string::npos) {
        std::cout << "子串 " << sub << " 在索引位置 " << found << " 处被找到。" << std::endl;
    } else {
        std::cout << "未找到子串 " << sub << "。" << std::endl;
    }
    return 0;
}


输出:

347dede0b18342059ab018d3a24cc6dc.png

2、size_type find( const char *str, size_type index );

此函数用于在字符串中查找给定 C-风格字符串 str 的第一个位置。它接收两个参数:str 是要查找的C-风格字符串,index 是在哪个位置开始搜索。函数返回找到的子串的第一个字符的索引位置,如果找不到子串,则返回 string::npos。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "Hello, world!";
    const char* sub = "world";
    size_t found = str.find(sub, 0);  // 在整个字符串中搜索
    if (found != std::string::npos) {
        std::cout << "C-风格字符串 " << sub << " 在索引位置 " << found << " 处被找到。" << std::endl;
    } else {
        std::cout << "未找到 C-风格字符串 " << sub << "。" << std::endl;
    }
    return 0;
}


输出:

a62c89e871b34d72966c2e0bdcf6711f.png

3、size_type find( char ch, size_type index );

此函数用于在字符串中查找给定字符 ch 的第一个位置。它接收两个参数:ch 是要查找的字符,index 是在哪个位置开始搜索。函数返回找到的字符的索引位置,如果找不到字符,则返回 string::npos。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "Hello, world!";
    char ch = 'o';
    size_t found = str.find(ch, 0);  // 在整个字符串中搜索
    if (found != std::string::npos) {
        std::cout << "字符 " << ch << " 在索引位置 " << found << " 处被找到。" << std::endl;
    } else {
        std::cout << "未找到字符 " << ch << "。" << std::endl;
    }
    return 0;
}


输出:

ef6aa51b3e324a13aaeda308bfe4ab61.png


二、返回字串


1、substr( size_type index, size_type num = npos );

此函数用于截取原字符串的子串,并返回新的字符串作为结果。它接收两个参数:index 是要开始截取的索引位置,num 是要截取的字符数。参数 num 是可选的,如果未提供,则默认截取从 index 一直到字符串末尾的所有字符。函数的返回类型是 basic_string。注意,index 参数必须在有效范围内,否则可能会导致未定义行为。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "Hello, world!";
    std::string sub1 = str.substr(7);  // 从索引位置 7 开始截取到字符串末尾
    std::cout << "截取子串1: " << sub1 << std::endl;
    std::string sub2 = str.substr(7, 5); // 从索引位置 7 开始截取 5 个字符
    std::cout << "截取子串2: " << sub2 << std::endl;
    return 0;
}


输出:

a6e7e30641774ecc9f146e538667f9c7.png


三、交换字串


1、swap( basic_string &str );

此函数用于交换两个字符串的内容。它接收一个参数 str,是要交换内容的另一个字符串。该函数不返回任何值,它直接修改了原字符串和传入的字符串。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";
    std::cout << "交换前:" << std::endl;
    std::cout << "str1: " << str1 << std::endl;
    std::cout << "str2: " << str2 << std::endl;
    str1.swap(str2);  // 交换字符串内容
    std::cout << "交换后:" << std::endl;
    std::cout << "str1: " << str1 << std::endl;
    std::cout << "str2: " << str2 << std::endl;
    return 0;
}


输出:

f0b77b332b77469cbcede91c13165d5f.png


四、运算符重载


1、输入输出

<<:用于将字符串输出到流中。

>>:用于从流中读取字符串。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str;
    // 从标准输入读取字符串
    std::cout << "请输入一个字符串:";
    std::cin >> str;
    // 将字符串输出到标准输出
    std::cout << "输入的字符串为:" << str << std::endl;
    return 0;
}


2、下标运算

下标运算符 []:

用于访问字符串中指定索引位置的字符。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "Hello";
    // 访问字符串中的第一个字符
    char firstChar = str[0];
    std::cout << "第一个字符为:" << firstChar << std::endl;
    return 0;
}


3、赋值

+=:用于将右侧字符串附加到左侧字符串。

=:用于将右侧字符串赋值给左侧字符串。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "Hello";
    std::string str2 = " World!";
    // 使用+=将两个字符串连接起来
    str1 += str2;
    std::cout << "连接后的字符串为:" << str1 << std::endl;
    // 使用=将str2赋值给str1
    str1 = str2;
    std::cout << "赋值后的字符串为:" << str1 << std::endl;
    return 0;
}


4、比较

==
>
<
>=
<=
!=

用于比较两个字符串的大小关系。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";
    // 比较两个字符串是否相等
    if (str1 == str2) {
        std::cout << "字符串相等" << std::endl;
    } else {
        std::cout << "字符串不相等" << std::endl;
    }
    // 比较两个字符串的大小关系
    if (str1 > str2) {
        std::cout << "str1 大于 str2" << std::endl;
    } else if (str1 < str2) {
        std::cout << "str1 小于 str2" << std::endl;
    } else {
        std::cout << "str1 等于 str2" << std::endl;
    }
    return 0;
}


5、计算

+用于将两个字符串进行连接。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "Hello";
    std::string str2 = " World!";
    // 将两个字符串进行连接
    std::string result = str1 + str2;
    std::cout << "连接后的字符串为:" << result << std::endl;
    return 0;
}



总结


在本文中,我们深入浅出地介绍了C++ STL中的string类的三个基本操作:查找子串(find)、返回子串(substr)和交换操作(swap)。通过使用这些操作,您可以方便地在字符串中查找子串、提取子串并交换字符串内容。这些功能为您处理字符串提供了便利,可以增加程序的灵活性和功能。

希望通过这篇文章,您能更好地理解和掌握C++的STL中的string类的操作,并在实际开发中灵活运用。祝您编程愉快!

0c49dfba68184dbd96b109c046caaff2.png

相关文章
|
2天前
|
编译器 C++
【C++】一文全解四种经典 [ 特殊类 ]的设计
【C++】一文全解四种经典 [ 特殊类 ]的设计
|
3天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-3
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-2
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
存储 编译器 C语言
c++初阶------类和对象(六大默认构造函数的揭破)-1
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
存储 编译器 C语言
c++初阶-------类和对象-2
c++初阶-------类和对象
|
3天前
|
编译器 C语言 C++
c++初阶-------类和对象-1
c++初阶-------类和对象
|
4天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
4天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
4天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
4天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现