【C++STL基础入门】深入浅出string类的比较(compare)、复制(copy)

简介: 【C++STL基础入门】深入浅出string类的比较(compare)、复制(copy)

前言


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

在C++标准库中,string类是一个功能强大的字符串处理类,提供了丰富的操作函数。本文将详细介绍string类的比较、复制、查找字串、返回字串、交换等常用操作。通过深入浅出的解析,让读者对这些操作有更清晰的理解和掌握。


一、比较


1.比较运算符

1、重载比较运算符, 结果是真和假。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";
    if (str1 == str2) {
        std::cout << "str1 and str2 are equal." << std::endl;
    } else {
        std::cout << "str1 and str2 are not equal." << std::endl;
    }
    return 0;
}


输出:

50da88d642094e36beb10f2441003d52.png


2.compare函数

1、int compare( const basic_string &str );

作用:用于比较当前字符串对象与给定的 basic_string 对象 str 是否相等。

参数:str 是要进行比较的目标字符串对象。

返回值:返回一个整数,表示比较结果。如果当前字符串小于 str,则返回一个负数;如果当前字符串等于 str,则返回 0;如果当前字符串大于 str,则返回一个正数。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "apple";
    std::string str2 = "banana";
    int result = str1.compare(str2);
    if (result < 0) {
        std::cout << "str1 is less than str2." << std::endl;
    } else if (result > 0) {
        std::cout << "str1 is greater than str2." << std::endl;
    } else {
        std::cout << "str1 is equal to str2." << std::endl;
    }
    return 0;
}


d025bf008c3d46ef9d648300fe33d6f0.png


2、int compare( const char *str );

作用:用于比较当前字符串对象与以 null 结尾的 C-Style 字符串 str 是否相等。

参数:str 是要进行比较的 C-Style 字符串。

返回值:返回一个整数,表示比较结果。如果当前字符串小于 str,则返回一个负数;如果当前字符串等于 str,则返回 0;如果当前字符串大于 str,则返回一个正数。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "hello";
    int result = str.compare("world");
    if (result < 0) {
        std::cout << "str is less than \"world\"." << std::endl;
    } else if (result > 0) {
        std::cout << "str is greater than \"world\"." << std::endl;
    } else {
        std::cout << "str is equal to \"world\"." << std::endl;
    }
    return 0;
}


8da5df1915e54db5b7d70594f3f4d2e8.png


3、int compare( size_type index, size_type length, const basic_string &str );

作用:用于从当前字符串的指定位置开始,比较 length 个字符与给定的 basic_string 对象 str 是否相等。

参数:index 是当前字符串的起始位置,length 是要比较的字符数,str 是要进行比较的目标字符串对象。

返回值:返回一个整数,表示比较结果。如果子字符串小于 str,则返回一个负数;如果子字符串等于 str,则返回 0;如果子字符串大于 str,则返回一个正数。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str = "helloworld";
    int result = str.compare(0, 5, "hello");
    if (result < 0) {
        std::cout << "The substring is less than \"hello\"." << std::endl;
    } else if (result > 0) {
        std::cout << "The substring is greater than \"hello\"." << std::endl;
    } else {
        std::cout << "The substring is equal to \"hello\"." << std::endl;
    }
    return 0;
}

5888103d4164429f89b0fbcdd39b255e.png

4、int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 );

作用:用于从当前字符串的指定位置开始,比较 length 个字符与给定的 basic_string 对象 str 的子字符串是否相等。

参数:index 是当前字符串的起始位置,length 是要比较的字符数,str 是要进行比较的目标字符串对象,index2 是目标字符串的起始位置,length2 是要比较的目标字符串的字符数。

返回值:返回一个整数,表示比较结果。如果子字符串小于目标字符串的子字符串,则返回一个负数;如果子字符串等于目标字符串的子字符串,则返回 0;如果子字符串大于目标字符串的子字符串,则返回一个正数。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string str1 = "helloworld";
    std::string str2 = "ello";
    int result = str1.compare(1, 5, str2, 1, 3);
    if (result < 0) {
        std::cout << "The substring is less than the substring of str2." << std::endl;
    } else if (result > 0) {
        std::cout << "The substring is greater than the substring of str2." << std::endl;
    } else {
        std::cout << "The substring is equal to the substring of str2." << std::endl;
    }
    return 0;
}


11ac8133eca647f29a0ec30764f4baae.png


二、复制


1.copy函数

1、size_type copy( char *str, size_type num, size_type index );

作用是从调用函数的字符串中复制指定数量的字符到目标字符数组中。

参数的作用如下:

str:指向目标字符数组的指针,用于存储复制的字符。

num:要复制的最大字符数。

index:开始复制的位置索引。

返回值的作用:

size_type:实际复制的字符数。如果这个值小于 num,表示被复制字符串的长度不足以满足要求。

示例代码:

#include <iostream>
#include <string>
int main() {
    std::string source = "hello world";
    char destination[10];
    size_t copiedChars = source.copy(destination, 5, 3);
    destination[copiedChars] = '\0';  // 添加字符串结束符
    std::cout << "复制的字符数: " << copiedChars << std::endl;
    std::cout << "目标字符数组中的内容: " << destination << std::endl;
    return 0;
}


5b24ddceb5ad472d86514e5dba1e8bb1.png

虽然有乱码但是不影响,我们可以通过断点来查看.

4ef8b0f420974f9cb945916e57574dfd.png


总结


本文深入浅出地介绍了string类的比较、复制常用操作。通过对这些操作的解析和示例,读者可以更加清晰地理解和掌握这些功能。熟练使用string类的操作函数,将有助于提高字符串处理的效率和代码质量,更好地应对实际开发需求。

2e30592b49d644c6b5016484eb5d2aaa.png

相关文章
|
7小时前
|
设计模式 算法 C++
【C++】STL之迭代器介绍、原理、失效
【C++】STL之迭代器介绍、原理、失效
13 2
|
7小时前
|
C++ Linux
|
7小时前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
14 4
|
7小时前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
17 0
|
7小时前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
7小时前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1
|
7小时前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
15 0
|
7小时前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
16 0
【C++】string学习 — 手搓string类项目
|
7小时前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)