首页 > 编程语言 >【算法】字符串函数

【算法】字符串函数

时间:2024-06-04 13:33:33浏览次数:13  
标签:函数 cout 算法 str19 字符串 World include size

今天讲讲字符串函数。

// C++标凇库提供了丰富的字符串操作函数,下面介绍一些常用的函数。
// 备注:位置可以看成是字符串的下标,从0开始
// 获取字符串长度
// 使用length或size函数来获取字符串的长度。
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

int main() {
    string str = "Hello, World!";
    size_t length = str.length(); // or str.size()
    cout << "Length of the string: " << length << endl;

    // 拼接字符串
    // 使用+操作符或append函数来拼接字符串。
    string str1 = "Hello";
    string str2 = "World";
    string str3 = str1 + ", " + str2 + "!";
    cout << str3 << endl;

    str1.append(", ").append(str2).append("!");
    cout << str1 << endl;

    // 查找子字符串
    // 使用find函数来查找子字符串的位置。没找到则返回-1
    string str4 = "Hello, World!";
    size_t pos = str4.find("World");
    if (pos != -1) {
        cout << "Found 'World' at position: " << pos << endl;
    } else {
        cout << "'World' not found" << endl;
    }

    // 替换子字符串
    // 使用replace函数来替换子字符串。
    string str5 = "Hello, World!";
    str5.replace(7, 5, "C++"); // 从位置7开始,替换长度为5的子字符串(这5个字符将被删除)
    cout << str5 << endl;// "Hello, C++!"

    // 子字符串提取
    // 使用substr函数提取子字符串。
    string substr = str5.substr(7, 5); // 从位置7开始,提取长度为5的子字符串
    cout << substr << endl;

    // 清空字符串
    // 使用clear函数来清空字符串。
    string str6 = "Hello, World!";
    str6.clear();
    cout << "After clear: " << str6 << endl;

    // 字符串是否为空
    // 使用empty函数检查字符串是否为空。
    string str7 = "";
    if (str7.empty()) {
        cout << "The string is empty" << endl;
    } else {
        cout << "The string is not empty" << endl;
    }

    // 访问字符
    // 使用索引操作符[]或at函数来访问字符串中的字符。
    string str8 = "Hello, World!";
    char ch1 = str8[0]; // 访问第一个字符
    char ch2 = str8.at(1); // 访问第二个字符

    cout << "First character: " << ch1 << endl;
    cout << "Second character: " << ch2 << endl;

    // 插入字符串
    // 使用insert函数在指定位置插入子字符串。
    string str9 = "Hello, World!";
    str9.insert(7, "C++ ");// 在s[7]位置插入字符串
    cout << str9 << endl; // 输出: Hello, C++ World!

    // 删除字符串
    // 使用erase函数删除指定位置的子字符串。
    string str10 = "Hello, C++ World!";
    str10.erase(7, 4); // 从位置7开始删除长度为4的子字符串
    cout << str10 << endl; // 输出: Hello, World!


    // 反转字符串
    // 虽然没有直接的函数,但可以使用标准库算法reverse来反转字符串。
    string str17 = "Hello, World!";
    reverse(str17.begin(), str17.end());
    cout << str17 << endl; // 输出: !dlroW ,olleH

    // 查找字符 - 暂不要求掌握
    // 使用find_first_of和find_last_of函数查找字符串中指定字符的第一个或最后一个位置。
    string str11 = "Hello, World!";
    size_t pos1 = str11.find_first_of('o');
    cout << "First occurrence of 'o': " << pos1 << endl; // 输出: 4

    pos1 = str11.find_last_of('o');
    cout << "Last occurrence of 'o': " << pos1 << endl; // 输出: 8

    // 查找不包含字符 - 暂不要求掌握
    // 使用find_first_not_of和find_last_not_of函数查找字符串中第一个或最后一个不包含指定字符的位置。
    string str12 = "Hello, World!";
    size_t pos2 = str12.find_first_not_of('H');
    cout << "First character not 'H': " << pos2 << endl; // 输出: 1

    pos2 = str12.find_last_not_of('!');
    cout << "Last character not '!': " << pos2 << endl; // 输出: 11

    // 转换大小写 - 暂不要求掌握
    // 没有直接的函数,但可以使用标准库函数来转换字符串中的字符。
    string str13 = "Hello, World!";
    transform(str13.begin(), str13.end(), str13.begin(), ::toupper);
    cout << "Uppercase: " << str13 << endl; // 输出: HELLO, WORLD!

    transform(str13.begin(), str13.end(), str13.begin(), ::tolower);
    cout << "Lowercase: " << str13 << endl; // 输出: hello, world!

    // 比较字符串 - 暂不要求掌握
    // 使用compare函数比较两个字符串。
    string str14 = "Hello";
    string str15 = "World";
    int result = str14.compare(str15);
    if (result < 0) {
        cout << "str14 is less than str15" << endl;
    } else if (result > 0) {
        cout << "str14 is greater than str15" << endl;
    } else {
        cout << "str14 is equal to str15" << endl;
    }

    // 转换为C风格字符串 - 暂不要求掌握
    // 使用c_str函数获取C风格的字符串(即以空字符结尾的字符数组)。
    string str16 = "Hello, World!";
    const char* cstr = str16.c_str();
    cout << cstr << endl; // 输出: Hello, World!

    // 查找子字符串 - 暂不要求掌握
    // 除了find之外,还有rfind函数从右向左查找子字符串。
    string str18 = "Hello, World!";
    size_t pos3 = str18.rfind("World");
    if (pos3 != string::npos) {
        cout << "Found 'World' at position: " << pos3 << endl; // 输出: Found 'World' at position: 7
    } else {
        cout << "'World' not found" << endl;
    }

    // 判断前缀和后缀 - 暂不要求掌握
    // 使用starts_with和ends_with函数判断字符串是否以特定前缀或后缀开头或结尾(C++20)。
    #if __cplusplus >= 202002L // Check if C++20
    string str19 = "Hello, World!";
    bool starts = str19.starts_with("Hello");
    bool ends = str19.ends_with("World!");
    cout << "Starts with 'Hello': " << (starts ? "Yes" : "No") << endl; // 输出: Yes
    cout << "Ends with 'World!': " << (ends ? "Yes" : "No") << endl; // 输出: Yes
    #endif

    return 0;
}

标签:函数,cout,算法,str19,字符串,World,include,size
From: https://blog.csdn.net/yangyanbin_sam/article/details/139440291

相关文章

  • 【会议征稿,IEEE出版】第二届算法、图像处理与机器视觉国际学术会议(AIPMV2024,7月12-14)
    2024年镇江市计算机科学技术大会暨第二届算法、图像处理与机器视觉国际学术会议(AIPMV2024)将于2024年7月12日-14日在江苏镇江召开。会议主要围绕算法、图像与视觉处理等研究领域展开讨论,为从事算法、图像与视觉处理研究的专家学者、工程技术人员、技术研发人员提供一个分享......
  • 【耗时8个小时整理】硬核集成算法,学习完你会哭着感谢自己!
    纯 干 货目录纯 干 货1、Bagging(自举汇聚法)2、Boosting(提升法)3、Stacking(堆叠法)4、Voting(投票法)5、深度学习集成今天给大家带来的是「集成算法」的全部整理!其实今儿的一些内容比较好理解~集成算法(EnsembleLearning)是一种将多个弱学习器......
  • 程序员最趁手的SVM算法,学完你会哭着感谢努力的自己!
    纯 干 货目录纯 干 货1线性支持向量机2非线性支持向量机3多类别支持向量机4核函数支持向量机5稀疏支持向量机6核贝叶斯支持向量机7不平衡类别支持向量机在这之前咱们已经接触了各个算法的优缺点的总结,以及8个回归类算法、7个正则化......
  • 代码随想录算法训练营第二十四天 | 回溯算法 77.组合
    回溯算法理论基础文章讲解视频讲解回溯是递归的副产品,只要有回溯就会有递归回溯的本质是琼剧,所以效率不高回溯法可以解决的问题组合问题切割问题子集问题排列问题棋盘问题如何理解回溯回溯算法的问题都可以抽象为树形结构集合的大小就构成了书的快读,递归的深度......
  • 在JavaScript中,函数前加`async`和不加`async`的区别
    在JavaScript中,函数前加async和不加async主要有以下区别:返回值类型:加async:使用async关键字声明的函数总是返回一个Promise对象。这意味着,无论函数中返回的是什么值,它都会被自动包装在一个Promise中。不加async:普通函数返回的是其执行结果,它不会自动包装在Promise中。如果需......
  • 暗水印——变换域DCT水印算法(一种通用性强,能有抵御攻击的手段)
     随着计算机和网络技术的飞速发展,信息的安全保护问题日益突出。数字图像、音频和视频等多媒体数字产品愈来愈需要一种有效的版权保护方法——水印技术,通常用于保护知识产权、防止未经授权的访问、作弊等。广义上可以把水印技术划分为四大类:图像水印、视频水印、音频水印和......
  • 数据库(20)——日期函数
    常见函数函数功能CURDATE()返回当前日期CURTIME()返回当前时间NOW()返回当前日期和时间YEAR(date)获取指定date的年份MONTH(date)获取指定date的月份DAY(date)获取指定date的日期DATE_ADD(date,INTERVALexprtype)返回一个日期/时间值加上一个时间间隔expr后的时间值DATEDIFF......
  • python 字节转化为字符串
    str()在字节转化为字符串时,需要显式地提供encoding参数,否则返回的非正式的字符串表示。官方文档是这样说的:还可以使用decode()方法来将字节转化为字符串参考资料:Pythonstr()——将数据转换为字符串(freecodecamp.org)来自为知笔记(Wiz)......
  • 代码随想录算法训练营Day60 | 84.柱状图中最大的矩形 | Python | 个人记录向
    注:今天是代码随想录训练营的最后一天啦!!!本文目录84.柱状图中最大的矩形做题看文章以往忽略的知识点小结个人体会84.柱状图中最大的矩形代码随想录:84.柱状图中最大的矩形Leetcode:84.柱状图中最大的矩形做题无思路。看文章与42.接雨水很像,42.接雨水是找每个......
  • Carmack的快速开平方根倒数算法(Fast inverse square root)
    基本原理需求\(y=\frac{1}{\sqrt{x}}\)\(log(a^b×a^c)=bloga+cloga=(b+c)loga\)32位浮点表示法:二进制的科学计数法符号位1+阶码8(有符号的反码表示幂指数)+小数位23(二进制小数首位必为1,默认,只需表示小数位即可)-20240511163945890.webp)字符串形式:\(S_0​E_1​E_2​...E_7......