黑马c++ STL部分 笔记(2) string容器

简介: 黑马c++ STL部分 笔记(2) string容器

char*是指针

string是类,类内部封装了char*,管理这个字符串,是一个char*型的容器

函数:find,copy,delete,replace,insert等

1.构造string

// string的构造方式:
// 1 string() 创建一个空字符串 =string str
// 2 string(const char* s) 用s初始化
// 3 string(const string& str) 拷贝构造
// 4 string(int n,char c) n个字符c初始化
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  string s1; // 1 默认构造
  const char *str = "hello world";
  string s2(str);
  cout << "s2= " << s2 << endl; // 2 用s初始化 这里没有const是因为str已经是常量了
  string s3(s2);
  cout << "s3= " << s3 << endl; // 3 拷贝构造 不用const原因同2
  string s4(10, 'a');
  cout << "s4= " << s4 << endl; // 4 n个字符c初始化
}
int main()
{
  test01();
}


2.string的赋值

// string的赋值操作
// 1 string& operator=(const char* s);//char*类型字符串,赋值给当前字符串
// 2 string& operator=(const string &s);//把字符串s赋给当前字符串
// 3 string& operator=(char c);//字符赋给当前字符串
// 4 string& assign(const char* s);//同1
// 5 string& assign(const char* s,int n);把字符串s前n个字符赋给当前字符串
// 6 string& assign(const string &s);//同2
// 7 string& assign(int n,char c);//n个字符c赋给字符串
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  string str1;
  str1 = "hello world"; // 1
  // cout<<str1;//hello world
  string str2 = str1; // 2
  // cout<<str2;//hello world
  string str3;
  str3 = 'a'; // 3
  // cout<<str3;//a
  string str4;
  str4.assign("hello c++"); // 4
  // cout<<str4;//hello c++
  string str5;
  str5.assign("hello c++", 5); // 5
  // cout<<str5;//hello
  string str6;
  str6.assign(str5); // 6
  // cout<<str6;//hello
  string str7;
  str7.assign(10, 'b'); // 7
  cout << str7;         // bbbbbbbbbb
}
int main()
{
  test01();
}


3.string的拼接

// string字符串拼接:实现在字符串末尾拼接字符串
// 1 string& operator+=(const char* str)重载+=操作符
// 2 string& operator+=(const char c)重载+=操作符
// 3 string& operator+=(const string& str)重载+=操作符
// 4 string& append(const char* s)把字符串s连接到当前字符串结尾
// 5 string& append(const char* s,int n)把字符串s的前n个字符连接到当前字符串的结尾
// 6 string& append(const string &s)同3operator+=(const string& str)
// 7 string& append(const string &s,int pos,int n)把字符串s中从pos开始的n个字符连接到字符串结尾
 
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  string str1 = "我";
  str1 += "爱玩游戏"; // 1
  // cout<<str1;//我爱玩游戏
  str1 += ':'; // 2
  // cout<<str1;//2我爱玩游戏:
  string str2 = " LOL DNF";
  str1 += str2; // 3
  // cout<<str1;//我爱玩游戏: LOL DNF
  string str3 = "I";
  str3.append(" love "); // 4
  // cout << str3;// I love
  str3.append("game abcde", 4); // 5
  // cout << str3;// I love game
   str3.append(str2);//6
  // cout<<str3;//I love game LOL DNF
  string str4="I love game";
  str4.append(str2, 0, 4); // 7
  //cout << str4;// I love game LOL
   string str5="I love game";
  str5.append(str2, 4, 3); // 7
  cout << str5;// I love game DNF
}
int main()
{
  test01();
}


4.string的查找与替换  

// string的查找和替换
// 1 int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
// 2 int find(const char* s, int pos = 0) const;     //查找s第一次出现位置,从pos开始查找
// 3 int find(const char* s, int pos, int n) const;  //从pos位置查找s的前n个字符第一次位置
// 4 int find(const char c, int pos = 0) const;      //查找字符c第一次出现位置
// 5 int rfind(const string& str,int pos = npos) const;//查找str最后一次位置,从pos开始查找
// 6 int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
// 7 int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
// 8 int rfind(const char c, int pos = 0) const;     //查找字符c最后一次出现位置
// 9 string& replace(int pos, int n, const string& str);//替换从pos开始n个字符为字符串str
// 10 string& replace(int pos, int n,const char* s);  //替换从pos开始的n个字符为字符串
 
/*find查找是从左往后找第一个,rfind从右往左找第一个
find找到字符串后返回查找的第一个字符位置,找不到返回-1
replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
*/
#include <bits/stdc++.h>
using namespace std;
// 查找
void test01()
{
  string str1 = "abcdefgde";
  cout << "1:  " << str1.find("de") << endl;  // 查"def"在str1的哪个位置 //3
  cout << "1:  " << str1.find("df") << endl;  // 查"df"在str1的哪个位置 //很大的数字 or -1
  cout << "5:  " << str1.rfind("de") << endl; // 7
}
// 替换
void test02()
{
  string str1 = "abcdefg";
  string str2 = str1.replace(1, 3, "1111");
  cout << "9:  " << str2; // a1111efg 不是a1111defg,因为d被覆盖
}
int main()
{
  test01();
  test02();
}


5.string字符串比较

// string字符串比较 一般比较string相等
// 1 int compare(const string &s) const;       //与字符串s比较
// 2 int compare(const char *s) const;        //与字符串s比较
/*string字符串比较(从左往右依次比较ASCLL码)
= 返回 0
> 返回 1
< 返回 -1*/
#include <bits/stdc++.h>
using namespace std;
// 比较
void test01()
{
  string str1 = "hello";
  string str2 = "hello";
  if (str1.compare(str2) == 0)
  {
    cout << "=" << endl; //=
  }
  str2 = "zello";
  if (str1.compare(str2) < 0)
  {
    cout << "<" << endl; //<
  }
  str2 = "aello";
  if (str1.compare(str2) > 0)
  {
    cout << ">" << endl; //>
  }
}
 
int main()
{
  test01();
}


6.string的存取  

// string字符存取
// 1 char& operator[](int n);//通过[]方式取字符
// 2 char& at(int n);//通过at方式取字符
#include <bits/stdc++.h>
using namespace std;
// 比较
void test01()
{
  // 访问字符
  string str1 = "hello";
  for (int i = 0; i < str1.size(); i++) // size返回字符串长度
  {
    cout << str1[i] << " "; // h e l l o
  }
  cout << endl;
  for (int i = 0; i < str1.size(); i++) // size返回字符串长度
  {
    cout << str1.at(i) << " "; // h e l l o
  }
  cout << endl;
  // 修改字符
  str1[0] = 'x';
  cout << str1 << endl; // xello
  str1.at(0) = 'y';
  cout << str1 << endl; // yello
}
 
int main()
{
  test01();
}


7.string的插入和删除  

// string的插入和删除
// 1 string& insert(int pos,const char* s);//在pos位置插入字符串
// 2 string& insert(int pos,const string& s);//在pos位置插入字符串
// 3 string& insert(int pos,int n,char c);//在pos位置插入n个字符c
// 4 string& erase(int pos,int n=npos);//删除从pos开始的n个字符
#include <bits/stdc++.h>
using namespace std;
 
void test01()
{ // 插入
  string str = "hello";
  str.insert(1, "111");
  cout << str << endl; // h111ello
  str.erase(1, 3);
  cout << str << endl; // hello
}
 
int main()
{
  test01();
}


8.string求字串

// string求字串
// string substr(int pos=0,int n=npos) const;返回由pos开始的n个字符串组成的字符串
#include <bits/stdc++.h>
using namespace std;
 
void test01()
{
  string str = "abcdef";
  string subStr = str.substr(1, 3);
  cout << subStr << endl; // bcd
}
// 使用操作
void test02()
{
  string email = "zhangsan@sina.com";
  // 从邮箱中获取用户名信息
  int length = email.find("@");
  string username = email.substr(0, length); // length=8
  cout << username << endl;                  // zhangsan
}
int main()
{
  test01();
  test02();
}


相关文章
|
22天前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
5月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
117 2
|
4月前
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
72 0
|
5月前
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
96 5
|
5月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
115 2
|
7天前
|
安全 持续交付 云计算
课时5:阿里云容器服务:最原生的集成Docker和云服务
阿里云容器服务以服务化形式构建容器基础设施,大幅提升开发效率,简化应用部署流程。通过Docker容器和DevOps工具(如Jenkins),实现自动化部署与迭代,优化企业内部复杂部署问题。该服务支持GPU调度、混合云架构无缝迁移,并与阿里云产品体系无缝集成,提供安全防护、网络负载均衡等多重功能支持。凭借微服务架构,帮助企业突破业务瓶颈,提高资源利用率,轻松应对海量流量。
课时5:阿里云容器服务:最原生的集成Docker和云服务
|
27天前
|
Ubuntu API 网络虚拟化
ubuntu22 编译安装docker,和docker容器方式安装 deepseek
本脚本适用于Ubuntu 22.04,主要功能包括编译安装Docker和安装DeepSeek模型。首先通过Apt源配置安装Docker,确保网络稳定(建议使用VPN)。接着下载并配置Docker二进制文件,创建Docker用户组并设置守护进程。随后拉取Debian 12镜像,安装系统必备工具,配置Ollama模型管理器,并最终部署和运行DeepSeek模型,提供API接口进行交互测试。
335 15
|
3月前
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
340 78
|
2月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
215 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
3月前
|
监控 Docker 容器
在Docker容器中运行打包好的应用程序
在Docker容器中运行打包好的应用程序

热门文章

最新文章