C++的string容器->基本概念、构造函数、赋值操作、字符串拼接、查找和替换、字符串比较、字符存取、插入和删除、子串

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

//string的构造函数
/*
-string();                      //创建一个空的字符串 例如: string str;
-string(const char* s);           //使用字符串s初始化
-string(const string& str);   //使用一个string对象初始化另一个string对象
-string(int n, char c);      //使用n个字符c初始化
*/
void test01()
{
    string s1; //默认构造,创建空字符串,调用无参构造函数
    cout << "str1 = " << s1 << endl;

    const char* str = "hello world";
    string s2(str); //把c_string转换成了string

    cout << "s2 = " << s2 << endl;

    string s3(s2); //调用拷贝构造函数
    cout << "s3 = " << s3 << endl;

    string s4(10, 'a');
    cout << "s4 = " << s4 << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}

#include<iostream>
using namespace std;
#include <string>
//string赋值操作
/*
* string& operator=(const char* s);            //char*类型字符串 赋值给当前的字符串
* string& operator=(const string &s);          //把字符串s赋给当前的字符串
* string& operator=(char c);                   //字符赋值给当前的字符串
* string& assign(const char *s);               //把字符串s赋给当前的字符串
* string& assign(const char *s, int n);        //把字符串s的前n个字符赋给当前的字符串
* string& assign(const string &s);             //把字符串s赋给当前字符串
* string& assign(int n, char c);               //用n个字符c赋给当前字符串
*/
void test01()
{
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;

    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;

    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;

    string str4;
    str4.assign("hello c++");
    cout << "str4 = " << str4 << endl;

    string str5;
    str5.assign("hello c++",5);
    cout << "str5 = " << str5 << endl;

    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;

    string str7;
    str7.assign(5, 'x');
    cout << "str7 = " << str7 << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}

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

//string字符串拼接
/*
* string& operator+=(const char* str);                   //重载+=操作符
* string& operator+=(const char c);                      //重载+=操作符
* string& operator+=(const string& str);                 //重载+=操作符
* string& append(const char *s);                         //把字符串s连接到当前字符串结尾
* string& append(const char *s, int n);                  //把字符串s的前n个字符连接到当前字符串结尾
* string& append(const string &s);                       //同operator+=(const string& str)
* string& append(const string &s, int pos, int n);       //字符串s中从pos开始的n个字符连接到字符串结尾
*/
void test01()
{
    string str1 = "我";

    str1 += "爱玩游戏";

    cout << "str1 = " << str1 << endl;
    
    str1 += ':';

    cout << "str1 = " << str1 << endl;

    string str2 = "LOL DNF";

    str1 += str2;

    cout << "str1 = " << str1 << endl;

    string str3 = "I";
    str3.append(" love ");
    str3.append("game abcde", 4);
    //str3.append(str2);
    str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
    cout << "str3 = " << str3 << endl;
}
int main()
{
    test01();
    system("pause");
    return 0;
}

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

//string查找和替换
/*
* int find(const string& str, int pos = 0) const;           //查找str第一次出现位置,从pos开始查找
* int find(const char* s, int pos = 0) const;               //查找s第一次出现位置,从pos开始查找
* int find(const char* s, int pos, int n) const;            //从pos位置查找s的前n个字符第一次位置
* int find(const char c, int pos = 0) const;                //查找字符c第一次出现位置
* int rfind(const string& str, int pos = npos) const;       //查找str最后一次位置,从pos开始查找
* int rfind(const char* s, int pos = npos) const;           //查找s最后一次出现位置,从pos开始查找
* int rfind(const char* s, int pos, int n) const;           //从pos查找s的前n个字符最后一次位置
* int rfind(const char c, int pos = 0) const;               //查找字符c最后一次出现位置
* string& replace(int pos, int n, const string& str);       //替换从pos开始n个字符为字符串str
* string& replace(int pos, int n,const char* s);            //替换从pos开始的n个字符为字符串s
*/
void test01()
{
    //1.查找
    string str1 = "abcdefgde";
    int pos = str1.find("de");
    if (pos == -1)
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "pos = " << pos << endl;
    }
    //rfind和find区别
    //rfind从右往左查找   find从左往右查找
    pos = str1.rfind("de");
    cout << "pos = " << pos << endl;
}
void test02()
{
    //2.替换
    string str1 = "abcdefgde";
    //从1号位置起 3个字符 替换为“1111”
    str1.replace(1, 3, "1111");
    cout << "str1 = " << str1 << endl;
}
int main()
{
    test01();
    test02();
    system("pause");
    return 0;
}

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

//字符串比较
void test01()
{
    string s1 = "hello";
    string s2 = "aello";
    int ret = s1.compare(s2);
    if (ret == 0)
    {
        cout << "s1 等于 s2" << endl;
    }
    else if (ret > 0)
    {
        cout << "s1 大于 s2" << endl;
    }
    else
    {
        cout << "s1 小于 s2" << endl;
    }
}
int main()
{
    test01();
    system("pause");
    return 0;
}

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

//string字符存取
void test01()
{
    string str = "hello world";
    //1.通过[]访问单个字符
    for (int i = 0; i < str.size(); i++)
    {
        cout << str[i] << " ";
    }
    cout << endl;
    //2.通过at方式访问单个字符
    for (int i = 0; i < str.size(); i++)
    {
        cout << str.at(i) << " ";
    }
    cout << endl;
    //修改单个字符
    str[0] = 'x';
    str.at(1) = 'x';
    cout << str << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}

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

//字符串插入和删除
void test01()
{
    string str = "hello";
    //插入
    str.insert(1, "111");
    cout << str << endl;
    //删除
    str.erase(1, 3);  //从1号位置开始3个字符
    cout << str << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}

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

//string求子串
void test01()
{
    string str = "abcdefg";
    string subStr = str.substr(1, 3);
    cout << "subStr = " << subStr << endl;
    //实用操作
    string email = "hello@sina.com";
    int pos = email.find("@");
    string username = email.substr(0, pos);
    cout << "username: " << username << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2803902.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

前端JS学习(二):BOM、DOM对象与事件

Web API基本认知 Web API 的作用&#xff1a;使用JS去操作html和浏览器 Web API 的分类&#xff1a;DOM(网页文档对象模型)、BOM(浏览器对象模型) BOM BOM的全称是 Browser Object Model&#xff0c;浏览器对象模型。也就是 JavaScript 将浏览器的各个组成部分封装成了对象&…

修改单据转换规则后保存报错提示

文章目录 修改单据转换规则后保存报错提示 修改单据转换规则后保存报错提示

区块链与Solidity详细介绍及基本语法使用

一、区块链简介 区块链是一种分布式数据库技术&#xff0c;它以块的形式存储数据&#xff0c;并通过加密算法确保数据的安全性。每个块包含一系列交易&#xff0c;并通过哈希值与前一个块相连接&#xff0c;形成一个链式结构。这种结构使得数据难以被篡改&#xff0c;因为任何对…

2024.2.22 C++QT 作业

思维导图 练习题 1>完善对话框&#xff0c;点击登录对话框&#xff0c;如果账号和密码匹配&#xff0c;则弹出信息对话框&#xff0c;给出提示”登录成功“&#xff0c;提供一个Ok按钮&#xff0c;用户点击Ok后&#xff0c;关闭登录界面&#xff0c;跳转到其他界面。如果账…

线程池的基础使用和执行策略

什么是线程池 线程池&#xff0c;字面意思就是一个创建线程的池子&#xff0c;它的特点就是&#xff0c;在使用线程之前&#xff0c;就一次性把多个线程创建好&#xff0c;放到"池”当中。后面需要执行任务的时候&#xff0c;直接从"线程池"当中通过线程执行。…

通俗易懂分析:Vite和Webpack的区别

1、对项目构建的理解 先从浏览器出发&#xff0c; 浏览器是由浏览器内核和JS引擎组成&#xff1b;浏览器内核编译解析html代码和css代码&#xff0c;js引擎编译解析JavaScript代码&#xff1b;所以从本质上&#xff0c;浏览器只能识别运行JavaScript、CSS、HTML代码。 而我们在…

MATLAB环境下基于短时傅里叶变换和Rényi熵的脑电信号和语音信号分析

傅里叶变换是不能很好的反映信号在时域的某一个局部范围的频谱特点的&#xff0c;这一点很可惜。因为在许多实际工程中&#xff0c;人们对信号在局部区域的特征是比较关心的&#xff0c;这些特征包含着十分有用的信息。这类信号因为在时域(或者是空间域)上具有突变的非稳定性和…

Java基于SSM+JSP的超市进销库存管理系统

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

深入了解Git

1.1 Git 的工作流程简介 克隆 Git 资源作为工作目录 在克隆的资源上添加或修改文件 如果其他人修改了&#xff0c;你可以更新资源 在提交前查看修改 提交修改 在修改完成后&#xff0c;如果发现错误&#xff0c;可以撤回提交并再次修改并提交 1.2 Git 工作区、暂存区和版…

自存 angular material design 表单输入框lable右对齐样式

单个输入框的文字lable放输入框左边实现 material design 的组件库示例没有文字描述放左边的样式 ,所以mat-lable并没有放在mat-form-field中 <div class"input-container col-6"><mat-label>商品售价<span class"text-error">*</spa…

【springBoot】springAOP

AOP的概述 AOP是面向切面编程。切面就是指某一类特定的问题&#xff0c;所以AOP也可以理解为面向特定方法编程。AOP是一种思想&#xff0c;拦截器&#xff0c;统一数据返回和统一异常处理是AOP思想的一种实现。简单来说&#xff1a;AOP是一种思想&#xff0c;对某一类事务的集…

基于springboot+vue的中小型医院网站(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

TDesign Vue Next Starter中后台项目的生产环境部署与CSP内容安全策略、CORS跨源资源共享和服务后端开发

TDesign Vue Next Starter中后台项目的生产环境部署与CSP内容安全策略、CORS跨源资源共享和服务后端开发 目录 TDesign Vue Next Starter中后台项目的生产环境部署与CSP内容安全策略、CORS跨源资源共享和服务后端开发 一、TDesign Vue Next Starter中后台项目模板 1.1、项目…

Ubuntu20.04 查看系统版本号

目录 uname -auname -vlsb_release -acat /etc/issuecat /proc/version uname -a 查看系统发行版本号和操作系统版本 uname -v 查看版本号 lsb_release -a 查看发行版本信息 cat /etc/issue 查看系统版本 cat /proc/version 查看内核的版本号

vivado FSM Components

Vivado合成功能 •同步有限状态机&#xff08;FSM&#xff09;组件的特定推理能力。 •内置FSM编码策略&#xff0c;以适应您的优化目标。 •FSM提取默认启用。 •使用-fsm_extraction off可禁用fsm提取。 FSM描述 Vivado综合支持Moore和Mealy中的有限状态机&#xff08;…

ROX amine,Rhodamine X amine,可和羧酸等官能团反应

​您好&#xff0c;欢迎来到新研之家 文章关键词&#xff1a;Rhodamine X amine, ROX amine &#xff0c;Rhodamine X amine&#xff0c; ROX NH2&#xff0c;氨基ROX &#xff0c;ROX氨基&#xff0c;Rhodamine X氨基 一、基本信息 【产品简介】&#xff1a;ROX can also b…

C++的deque容器->基本概念、构造函数、赋值操作、大小操作、插入和删除、数据存取、排序

#include<iostream> using namespace std; #include <deque> //deque构造函数 void printDeque(const deque<int>& d) { for (deque<int>::const_iterator it d.begin(); it ! d.end(); it) { //*it 100; 容器中的数据不可以修…

DiceCTF 2024 -- pwn

baby-talk 题目给了 Dockerfile&#xff0c;但由于笔者 docker 环境存在问题启动不起来&#xff0c;所以这里用虚拟机环境做了&#xff08;没错&#xff0c;由于不知道远程 glibc 版本&#xff0c;所以笔者远程也没打通&#xff09;笔者本地环境为 glibc 2.31-0ubuntu9.9。然后…

开发技术-Java 获取集合中元素下标并移动至指定位置

1. 说明 某些业务需要特定的元素在列表的最后或者指定位置展示。 2. 代码 import lombok.AllArgsConstructor; import lombok.Data;import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream;Data AllArgsConstructor class Student {St…

第七篇【传奇开心果系列】python的文本和语音相互转换库技术点案例示例:Sphinx自动电话系统(IVR)经典案例

传奇开心果博文系列 系列博文目录python的文本和语音相互转换库技术点案例示例系列 博文目录前言一、雏形示例代码二、扩展思路介绍三、Sphinx多语言支持示例代码四、Sphinx和语音合成库集成示例代码五、Sphinx语音识别前自然语言预处理示例代码六、Sphinx语音识别自动电话系统…