【C++常用容器】STL基础语法学习&string容器

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: string是c++风格的字符串,而string本质上是一个类

●string基本概念


     

■本质
                string是c++风格的字符串,而string本质上是一个类
        ■string和char*区别
                char*是一个指针
                string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
        ■特点
                string类内部封装了很多成员方法
              (例如:查找find,拷贝copy,删除delete,替换replace,插入insert)
                string管理char*所分配的内存,不需要担心复制越界取值越界等,由类内进行负责


●string构造函数


构造函数原型:


      ▲ string() //创建一个空的字符串


      ▲string(const char* s)//使用字符串s初始化


      ▲string(const string& str)//使用一个string对象初始化另一个string对象


      ▲ string(int n,char c)//使用n个字符c初始化

#include<iostream>
#include<string>
using namespace std;
void text()  //测试代码
{
  string s1; 
  cout << "s1= "<<s1 << endl;
  const char* s = "hello world"; 
  string s2(s);
  cout << "s2= " << s2 << endl;
  const string& str=s2;
  string s3(str);
  cout << "s3= " << s3 << endl;
  string s4(10, 'a');
  cout << "s4= " << s4 << endl;
}
int main()
{
  text();
}

d3bf9ec69d541ab7fb010d51f3957e2e_e58b93e682de46f0bbe876d4167a151d.png


●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 string& s) //把字符串s赋给当前字符串


       ▲string& assign (const char* s,int n)   //把字符串s的前n个字符赋给当前的字符串


       ▲string& assign (int n,char c) //用n个字符c赋给当前字符串


#include<iostream>
#include<string>
using namespace std;
void text()
{
  string str1; 
  str1 = "hello world";
  cout << "str1= " << str1 << endl;
  string str2; 
  str2= str1;
  cout << "str2= " << str2 << endl;
  string str3;
  str3= 'c';
  cout << "str3= " << str3 << endl;
  string str4;
  str4.assign("hello world");
  cout << "str4= " << str4 << endl;
  string str5;
  str5.assign(str4);
  cout << "str5= " << str5 << endl;
  string str6;
  str6.assign("hello world", 5);
  cout << "str6= " << str6 << endl;
  string str7;
  str7.assign(10, 'a');
  cout << "str7= " << str7 << endl;
}
int main()
{
  text();
}

54882125fcbeb2807f45c28a1c916204_b27d75bd6c7e4c8794fb0a218489fa5d.png


●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个字符连接到字符串结尾

#include<iostream>
#include<string>
using namespace std;
void text()
{
  string str1 = "你好";
  str1 += "世界";
  cout << "str1= " << str1 << endl;
  string str2 = "你好";
  str2 += 'a';
  cout << "str2= " << str2 << endl;
  string  str3 = "";
  str3 += str1;
  cout << "str3= " << str3 << endl;
  string str4 = "hello";
  str4.append("world");
  cout << "str4= " << str4 << endl;
  string str5 = "hello";
  str5.append("world", 5);
  cout << "str5= " << str5 << endl;
  string str6 = "";
  str6.append(str5);
  cout << "str6= " << str6 << endl;
  string str7 = "";
  str7.append(str6, 0, 10);
  cout << "str7= " << str7 << endl;
}
int main()
{
  text();
}

2802753a49dfbea40124b776592b76b8_f7f31c94477d4356bcff852499ad1d29.png


●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最后一次出现位置


       ▲int replace(int pos,int n,const string& str);  //替换从pos开始n个字符为字符串str


       ▲int replace(int pos,int n,const char* s);  //替换从pos开始的n个字符为字符串s


#include<iostream>
#include<string>
using namespace std;
void text1() //查找
{
  string str1 = "helloworld";
  //find 从左往右查找
  int pos1 = str1.find("o");
  cout << "pos1= " << pos1 << endl;
  //rfind 从右往左查找
  int pos2 = str1.rfind("o");
  cout << "pos2= " << pos2 << endl;
}
void text2() //替换
{
  string str2 = "helloworld";
  str2.replace(1,3,"111");
  cout << "str2= " << str2 << endl;
}
int main()
{
  text1();
  text2();
}

585041ab06844cf6d62103abadf6c621_2baab7ef7ceb44c2972626445d829c29.png

●string字符串比较


比较方式:


       ▲字符串比较是按字符的ASCII码进行比较的


            = 返回 0


            > 返回 1


            < 返回 -1


 比较函数原型:


       ▲int compare(const string& str)  //与字符串str比较


       ▲int compare(const char* s) //与字符串s比较


#include<iostream>
#include<string>
using namespace std;
void text()
{
  string str1 = "hello";
  string str2 = "helloworld";
  if (str1.compare(str2) == 0) {
  cout << "str1=str2" <<endl;
  }
  else if (str1.compare(str2) > 0) {
  cout << "str1>str2" << endl;
  }
  else {
  cout << "str1<str2" << endl;
  }
}
int main()
{
  text();
}

37b0bffdefc3ae846a48068af22c900f_56fae62be42d4b859faf09e0ee56294b.png


●string字符存取


函数原型:


       ▲char& operator[ ](int n) //通过[ ]方式获取字符


       ▲char& at(int n) //通过at方法获取字符


#include<iostream>
#include<string>
using namespace std;
void text()
{ 
  //用[ ]来获取字符,并且用[ ]来进行修改
  string str1 = "hello";
  for (int i = 0; i < str1.size(); i++)
  {
  cout << str1[i] ;
  }
  cout << endl;
  str1[0] = 'a';
  cout << str1 << endl;
  //用at来获取字符,并且用at来进行修改
  string str2 = "hello";
  for (int i = 0; i < str2.size(); i++)
  {
  cout << str2.at(i);
  }
  cout << endl;
  str2.at(0) = 'a';
  cout << str2 << endl;
}
int main()
{
  text();
}

440412651f39fb7d89b8eaa9224acd9b_a615cf2ed1fd4e26ad642bf5b15ac82a.png


●string插入和删除


函数原型:


       ▲string& insert(int pos,const char* s)  //插入字符串


       ▲string& insert(int pos,const string& str) //插入字符串


       ▲string& insert(int pos,int n,char c)  //在指定位置插入n个字符


       ▲string& erase(int pos,int n=npos)  //删除从pos开始的n个字符


#include<iostream>
#include<string>
using namespace std;
void text()
{
  string str1 = "hello";
  str1.insert(5,"world"); //在hello第五个字符后插入字符串world
  cout << "str1= " << str1 << endl;
  str1.insert(5, 1, 'x');
  cout << "str1= " << str1 << endl; //在hello在五个字符后插入字符x
  str1.erase(5, 6);
  cout << "str1= " << str1 << endl; //删除hello第五个字符后的内容
}
int main()
{
  text();
}

ad66cf3f0719f38120eeb9b9fd8e8971_19f5aaed3b8042469dc62d8065506a12.png


●string字串


函数原型:


       ▲string substr(int pos=0,int n=npos) const;  //返回由pos开始的n个字符组成的字符串


#include<iostream>
#include<string>
using namespace std;
void text()
{
  string str1 = "hello";
  string str2 = str1.substr(1, 3);
  cout << "str2 = " << str2 << endl;
}
int main()
{
  text();
}

feebe72d25beb60698064069a132f40b_2b8b590c50744dceae389b6c332523df.png

目录
相关文章
|
10天前
|
对象存储 C++ 容器
c++的string一键介绍
这篇文章旨在帮助读者回忆如何使用string,并提醒注意事项。它不是一篇详细的功能介绍,而是一篇润色文章。先展示重载函数,如果该函数一笔不可带过,就先展示英文原档(附带翻译),最后展示代码实现与举例可以直接去看英文文档,也可以看本篇文章,但是更建议去看英文原档。那么废话少说直接开始进行挨个介绍。
|
4月前
|
存储 安全 C语言
C++ String揭秘:写高效代码的关键
在C++编程中,字符串操作是不可避免的一部分。从简单的字符串拼接到复杂的文本处理,C++的string类为开发者提供了一种更高效、灵活且安全的方式来管理和操作字符串。本文将从基础操作入手,逐步揭开C++ string类的奥秘,帮助你深入理解其内部机制,并学会如何在实际开发中充分发挥其性能和优势。
|
4月前
|
存储 缓存 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 的奥秘,从入门到高效编程
|
4月前
|
C++
模拟实现c++中的string
模拟实现c++中的string
|
4月前
|
编译器 C语言 C++
☺初识c++(语法篇)☺
☺初识c++(语法篇)☺
|
7月前
|
C语言 C++ 容器
【c++丨STL】string模拟实现(附源码)
本文详细介绍了如何模拟实现C++ STL中的`string`类,包括其构造函数、拷贝构造、赋值重载、析构函数等基本功能,以及字符串的插入、删除、查找、比较等操作。文章还展示了如何实现输入输出流操作符,使自定义的`string`类能够方便地与`cin`和`cout`配合使用。通过这些实现,读者不仅能加深对`string`类的理解,还能提升对C++编程技巧的掌握。
326 5
|
7月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
219 2
|
8月前
|
存储 安全 C++
【C++打怪之路Lv8】-- string类
【C++打怪之路Lv8】-- string类
69 1
|
8月前
|
C语言 C++
深度剖析C++string(中)
深度剖析C++string(中)
101 0
|
1月前
|
关系型数据库 MySQL Docker