【STL】string 基础,应用与操作

string

1.string相关介绍

STL(标准模板库)中的string容器是C++标准库提供的用于处理和操作字符串的类,位于头文件中。std::string提供了比传统的C风格字符串(字符数组)更方便和安全的功能,具有动态内存管理、丰富的操作函数和良好的兼容性。

2.string基础操作汇总

1.string的构造函数

  • string();
    //创建一个空的字符串 例如: string str;

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

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

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

2.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赋给当前字符

3.字符串拼接操作:实现在字符串末尾拼接字符串

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

4.string查找和替换

查找:查找指定字符串是否存在
替换:在指定的位置替换字符串

find从左往右查找,查找失败返回-1; 成功返回从左往右开始子串出现的一个位置索引

  • 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第一次出现位置

rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

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

利用find()和refind()可以查找 主串中 子串是否唯一

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

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

5.string字符串比较
比较方式:
字符串比较是按字符的ASCII码进行对比
等于: 返回 0
左大于右: 返回 1
左小于右: 返回 - 1

  • int compare(const string & s) const;
    //与字符串s比较

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

6.string字符存取

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

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

7.string插入和删除:对string字符串进行插入和删除字符操作

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

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

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

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

8.string子串:从字符串中获取想要的子串

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

3.string的创建与相关基础操作

3.1string的构造函数

  • string();
    //创建一个空的字符串 例如: string str;

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

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

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

//1.string的构造函数
void test1()
{
	//1.默认构造,空字符串
	string s1;

	const char * str= "hello world!";
	//2.用现成字符串初始化
	string s2(str);
	cout << "s2:" << s2 << endl;

	//3.拷贝构造
	string s3(s2);
	cout << "s3:" << s3 << endl;

	//4.指定构造
	string s4(10,'a');
	cout << "s4:" << s4 << endl;
}

在这里插入图片描述

3.2string 的赋值操作:给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赋给当前字符

//2.string 的赋值操作
void test2()
{
	//1.字符串直接赋值
	string s1;
	s1 = "hhhhhhhh";
	cout << s1 << endl;

	//2.拷贝赋值
	string s2;
	s2 = s1;
	cout << s2 << endl;

	//3.赋值单个字符
	string s3;
	s3 = 'a';
	cout << s3 << endl;

	
	//4.利用assign()赋值函数
	string s4;
	s4.assign("hello C++");
	cout << s4 << endl;


	//5.利用assign(n,c)函数:把字符串c前n个字符赋值给s5
	string s5;
	s5.assign("hello c++", 5);
	cout << s5 << endl;


	//6.拷贝赋值
	string s6;
	s6.assign(s5);
	cout << s6 << endl;


	//7.n个字符赋值
	string s7;
	s7.assign(10,'a');
	cout << s7 << endl;

}

在这里插入图片描述

3.3字符串拼接操作:实现在字符串末尾拼接字符串

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

//3.字符串拼接操作:实现在字符串末尾拼接字符串

//重载+=操作符:
void test03()
{
	string s = "我";

	//追加一个字符串
	s += "爱玩游戏";

	cout << s << endl;

	//追加单个字符

	s += '!';

	cout << s << endl;

	//追加一个string
	string s2 = "哈哈哈哈!";

	s += s2;

	cout << s << endl;
}

//append操作:
void test13()
{
	string s = "玛卡巴卡";

	string s2 = "哈哈哈哈";

	//追加一个字符串
	s.append("晚安!");
	cout << s << endl;

	//字符串 s的前n个字符连接到当前字符串结尾(注意第一个参数是const char *类型的,不是string类型的)
	s.append("abcde",3);//注意中至少占两个字节(一般就是两个字节)
	cout << s << endl;

	//追加一个string
	s.append(s2);
	cout << s << endl;

	string s3 = "aokk";
	//string s的前n个字符连接到当前字符串结尾(注意这里需要传入第二个参数:待追加string的开始索引)
	s.append(s3, 1, 2);
	cout << s << endl;
}

重载+=操作符:
在这里插入图片描述
append操作:
在这里插入图片描述

3.4string查找和替换

查找:查找指定字符串是否存在
替换:在指定的位置替换字符串

find从左往右查找,查找失败返回-1; 成功返回从左往右开始子串出现的一个位置索引

  • 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第一次出现位置

rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

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

利用find()和refind()可以查找 主串中 子串是否唯一

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

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

//4.string查找和替换

//查找:查找指定字符串是否存在
//通过find()和rfind()可以确定子串是否在主串中重复出现
//常用操作如下:
void test04()
{
	string s = "abcdefde";
	
	cout <<"主串:" << s << endl;

	cout << "find()从左往右查找:" << endl;
	//find
	//查找指定字符串是否在当前string中
	int pos1 = s.find("de");
	
	//未找到会返回-1
	if (pos1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout <<"子串de的起始索引为" << pos1 << endl;
	}
	
	int pos2 = s.find("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << pos2 << endl;
	}

	cout << endl << "rfind()从右往左查找:" << endl;
	//rfind
	//查找指定字符串是否在当前string中
	int po1 = s.rfind("de");

	//未找到会返回-1
	if (po1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串de的起始索引为" << po1 << endl;
	}

	int po2 = s.rfind("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << po2 << endl;
	}
}

//替换:在指定的位置替换字符串
void test14()
{
	string s = "abcdef";

	//从指定位置索引(int pos)处,往后(int n)个字符,替换成传入的字符串(string)整体
	//注意:这里替换的是传入的字符串整体
	s.replace(2, 3,"hhhhhh");//应该输出:abhhhhhhef
	cout << s << endl;
}

查找:
在这里插入图片描述
替换:
在这里插入图片描述

3.5string字符串比较

比较方式:
字符串比较是按字符的ASCII码进行对比
等于: 返回 0
左大于右: 返回 1
左小于右: 返回 - 1

  • int compare(const string & s) const;
    //与字符串s比较

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

 //5.string字符串比较
//字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
void test5()
{
	string s1 = "hello";
	string s2 = "hello";

	//字符串比较是按字符的ASCII码进行对比
	//= 返回 0
	//> 返回 1
	//< 返回 - 1
	if (s1.compare(s2) == 0)
	{
		cout << "s1 == s2" << endl;
	}
	else if (s1.compare(s2) > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}

在这里插入图片描述

3.6string字符存取

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

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

 //6.string字符存取
void test6()
{
	string s1 = "hello word!";

	cout << s1 << endl;

	//通过中括号来访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i];
	}
	cout << endl;

	//通过成员函数at()访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i);
	}
	cout << endl;



	//同样可以使用上述两种方法进行单个字符的修改
	s1[0] = 'H';
	s1.at(1) = 'E';
	cout << s1 << endl;
}

在这里插入图片描述

3.7string插入和删除:对string字符串进行插入和删除字符操作

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

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

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

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

//7.string插入和删除:对string字符串进行插入和删除字符操作
void test7()
{
	string s1 = "ok,hello";
	
	//指定位置插入字符串
	s1.insert(1, "map");//应该输出:omapk,hello
	cout << s1 << endl;

	//指定位置插入n个相同字符
	s1.insert(0, 7, 'f');
	cout << s1 << endl;//应该输出:fffffffomapk,hello


	//指定位置起删除n个字符
	s1.erase(0, 7);//应该输出:omapk,hello
	cout << s1 << endl;
}

在这里插入图片描述

3.8string子串:从字符串中获取想要的子串

  • string substr(int pos = 0, int n = npos) const;
    //返回由pos开始的n个字符组成的字符串
//8.string子串:从字符串中获取想要的子串
void test8()
{
	string s1 = "hello,world";

	//返回由pos开始的n个字符组成的字符串
	string s2 = s1.substr(1, 3);//应该输出:ell
	cout << s2 << endl;

	
	//实用操作:
	//从邮件地址中获取用户名
	string email = "zhangsan@qq.com";
	//获取@所处位置索引
	int pos = email.find("@");

	cout << email.substr(0, pos);//从0开始,截取pos个字符(因为字符串下标从0开始)
}

在这里插入图片描述

4.总结

4.1string的主要特性

1. 动态内存管理

std::string可以自动调整其大小,允许存储任意长度的字符串。它会根据需要分配内存,并在需要时扩展,不需要程序员手动管理内存。这种特性大大降低了内存管理的复杂性,避免了手动管理C风格字符串可能导致的缓冲区溢出问题。

std::string str = "Hello";
str += " World";  // 自动扩展内存以存储更长的字符串
2. 字符串操作的便利性

std::string提供了丰富的成员函数,如拼接、查找、替换、截取、插入、删除等,方便用户对字符串进行各种操作。这使得C++开发者可以更容易地进行复杂的字符串处理,而无需手动编写低级的操作逻辑。

3. C风格字符串的兼容性

std::string内部是基于动态数组管理字符的,但它提供了与C风格字符串兼容的接口,比如c_str()方法,返回一个指向内部字符数组的指针,可以用于C库或旧的C代码。

const char* cstr = s.c_str();  // 获取C风格的字符串指针
4. 支持迭代器和范围循环

std::string作为STL容器,支持随机访问迭代器和范围循环,可以方便地遍历字符串中的每个字符。

for (char ch : s) 
{
    std::cout << ch << " ";
}
5. 内存效率:小字符串优化

C++11标准引入了“小字符串优化”(Small String Optimization, SSO),即当字符串非常短时,std::string可能不会动态分配堆内存,而是将字符串直接存储在对象内部的固定空间中。这样可以避免频繁的内存分配操作,提升效率。

4.2std::string的注意事项

1. 内存和性能考虑

虽然std::string的动态内存管理提供了极大的方便性,但频繁的字符串操作(如拼接、插入、删除)可能会引起性能问题,尤其是涉及到多次重新分配和复制内容的情况。

  • 预留内存空间: 如果可以预见字符串的长度变化,使用reserve()方法预先分配足够的内存,以减少内存重新分配的次数,提升性能。
std::string s;
s.reserve(100);  // 预留100字符的空间,避免频繁扩展
  • 避免不必要的复制: 尽量使用引用传递或move语义来避免拷贝大字符串。例如,通过std::move可以避免不必要的拷贝构造:
std::string str1 = "Hello World";
std::string str2 = std::move(str1);  // str1被移动到str2,避免了复制
2. 使用c_str()的注意事项

c_str()返回的指针是指向std::string内部数据的常量指针,不允许修改。如果试图修改这个指针所指向的数据,会导致未定义行为。另外,c_str()返回的指针在字符串对象被销毁后或其内容发生改变时可能会失效。

std::string s = "Hello";
const char* cstr = s.c_str();
// 注意:不能修改cstr指向的内容
3. 字符串的越界访问

使用[]运算符访问字符串中的字符时,不会进行边界检查,这意味着如果索引超出字符串的范围,可能导致未定义行为。可以使用at()方法,它会进行边界检查,防止越界。

std::string s = "Hello";
char ch = s.at(10);  // 会抛出std::out_of_range异常
4. 避免重复计算字符串长度

std::string的size()或length()函数是常量时间操作,因为它们内部维护了长度值。即便如此,在长字符串或复杂代码中,频繁调用这些函数也可能会影响代码的可读性。建议在需要多次使用字符串长度时,将其保存到变量中。

size_t len = s.size();
for (size_t i = 0; i < len; ++i) 
{
    // 操作字符
}
  1. 使用find()时的边界检查
    find()返回的索引是size_t类型,如果未找到子串,则返回std::string::npos。在对返回值进行处理时,需要注意这个特殊值:
size_t pos = s.find("World");
if (pos != std::string::npos)
 {
    // 找到了子串
}

5.整体代码一览

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



//1.string的构造函数
//string();                             //创建一个空的字符串 例如: string str; 
//string(const char* s);                //使用字符串s初始化
//string(const string& str);            //使用一个string对象初始化另一个string对象
//string(int n, char c);                //使用n个字符c初始化


//2.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赋给当前字符


//3.字符串拼接操作:实现在字符串末尾拼接字符串
//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个字符连接到字符串结尾


//4.string查找和替换

//查找:查找指定字符串是否存在
//替换:在指定的位置替换字符串

//find从左往右查找,查找失败返回-1;  成功返回从左往右开始子串出现的一个位置索引

//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第一次出现位置

//rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

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

//利用find()和refind()可以查找 主串中 子串是否唯一


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

		   
//5.string字符串比较
//比较方式:
//字符串比较是按字符的ASCII码进行对比
//= 返回 0
//> 返回 1
//< 返回 - 1

//int compare(const string & s) const;   //与字符串s比较
//int compare(const char* s) const;      //与字符串s比较
			 

		
//6.string字符存取
//char& operator[](int n);            //通过[]方式取字符
//char& at(int n);                    //通过at方法获取字符
		

//7.string插入和删除:对string字符串进行插入和删除字符操作
//string& insert(int pos, const char* s);       //插入字符串
//string& insert(int pos, const string& str);   //插入字符串
//string& insert(int pos, int n, char c);       //在指定位置插入n个字符c
//string& erase(int pos, int n = npos);         // 删除从Pos开始的n个字符 
	

//8.string子串:从字符串中获取想要的子串
//string substr(int pos = 0, int n = npos) const;   //返回由pos开始的n个字符组成的字符串
			
		  

//1.string的构造函数
void test1()
{
	//1.默认构造,空字符串
	string s1;

	const char * str= "hello world!";
	//2.用现成字符串初始化
	string s2(str);
	cout << "s2:" << s2 << endl;

	//3.拷贝构造
	string s3(s2);
	cout << "s3:" << s3 << endl;

	//4.指定构造
	string s4(10,'a');
	cout << "s4:" << s4 << endl;
}



//2.string 的赋值操作
void test2()
{
	//1.字符串直接赋值
	string s1;
	s1 = "hhhhhhhh";
	cout << s1 << endl;

	//2.拷贝赋值
	string s2;
	s2 = s1;
	cout << s2 << endl;

	//3.赋值单个字符
	string s3;
	s3 = 'a';
	cout << s3 << endl;

	
	//4.利用assign()赋值函数
	string s4;
	s4.assign("hello C++");
	cout << s4 << endl;


	//5.利用assign(n,c)函数:把字符串c前n个字符赋值给s5
	string s5;
	s5.assign("hello c++", 5);
	cout << s5 << endl;


	//6.拷贝赋值
	string s6;
	s6.assign(s5);
	cout << s6 << endl;


	//7.n个字符赋值
	string s7;
	s7.assign(10,'a');
	cout << s7 << endl;

}





//3.字符串拼接操作:实现在字符串末尾拼接字符串

//重载+=操作符:
void test03()
{
	string s = "我";

	//追加一个字符串
	s += "爱玩游戏";

	cout << s << endl;

	//追加单个字符

	s += '!';

	cout << s << endl;

	//追加一个string
	string s2 = "哈哈哈哈!";

	s += s2;

	cout << s << endl;
}

//append操作:
void test13()
{
	string s = "玛卡巴卡";

	string s2 = "哈哈哈哈";

	//追加一个字符串
	s.append("晚安!");
	cout << s << endl;

	//字符串 s的前n个字符连接到当前字符串结尾(注意第一个参数是const char *类型的,不是string类型的)
	s.append("abcde",3);//注意中至少占两个字节(一般就是两个字节)
	cout << s << endl;

	//追加一个string
	s.append(s2);
	cout << s << endl;

	string s3 = "aokk";
	//string s的前n个字符连接到当前字符串结尾(注意这里需要传入第二个参数:待追加string的开始索引)
	s.append(s3, 1, 2);
	cout << s << endl;
}


//4.string查找和替换

//查找:查找指定字符串是否存在
//通过find()和rfind()可以确定子串是否在主串中重复出现
//常用操作如下:
void test04()
{
	string s = "abcdefde";
	
	cout <<"主串:" << s << endl;

	cout << "find()从左往右查找:" << endl;
	//find
	//查找指定字符串是否在当前string中
	int pos1 = s.find("de");
	
	//未找到会返回-1
	if (pos1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout <<"子串de的起始索引为" << pos1 << endl;
	}
	
	int pos2 = s.find("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << pos2 << endl;
	}

	cout << endl << "rfind()从右往左查找:" << endl;
	//rfind
	//查找指定字符串是否在当前string中
	int po1 = s.rfind("de");

	//未找到会返回-1
	if (po1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串de的起始索引为" << po1 << endl;
	}

	int po2 = s.rfind("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << po2 << endl;
	}
}

//替换:在指定的位置替换字符串
void test14()
{
	string s = "abcdef";

	//从指定位置索引(int pos)处,往后(int n)个字符,替换成传入的字符串(string)整体
	//注意:这里替换的是传入的字符串整体
	s.replace(2, 3,"hhhhhh");//应该输出:abhhhhhhef
	cout << s << endl;
}


//5.string字符串比较
//字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
void test5()
{
	string s1 = "hello";
	string s2 = "hello";

	//字符串比较是按字符的ASCII码进行对比
	//= 返回 0
	//> 返回 1
	//< 返回 - 1
	if (s1.compare(s2) == 0)
	{
		cout << "s1 == s2" << endl;
	}
	else if (s1.compare(s2) > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}


//6.string字符存取
void test6()
{
	string s1 = "hello word!";

	cout << s1 << endl;

	//通过中括号来访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i];
	}
	cout << endl;

	//通过成员函数at()访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i);
	}
	cout << endl;



	//同样可以使用上述两种方法进行单个字符的修改
	s1[0] = 'H';
	s1.at(1) = 'E';
	cout << s1 << endl;
}


//7.string插入和删除:对string字符串进行插入和删除字符操作
void test7()
{
	string s1 = "ok,hello";
	
	//指定位置插入字符串
	s1.insert(1, "map");//应该输出:omapk,hello
	cout << s1 << endl;

	//指定位置插入n个相同字符
	s1.insert(0, 7, 'f');
	cout << s1 << endl;//应该输出:fffffffomapk,hello


	//指定位置起删除n个字符
	s1.erase(0, 7);//应该输出:omapk,hello
	cout << s1 << endl;
}

//8.string子串:从字符串中获取想要的子串
void test8()
{
	string s1 = "hello,world";

	//返回由pos开始的n个字符组成的字符串
	string s2 = s1.substr(1, 3);//应该输出:ell
	cout << s2 << endl;

	
	//实用操作:
	//从邮件地址中获取用户名
	string email = "zhangsan@qq.com";
	//获取@所处位置索引
	int pos = email.find("@");

	cout << email.substr(0, pos);//从0开始,截取pos个字符(因为字符串下标从0开始)
}


int main()
{
	test1();
	test2();
	test03();
	test13();
	test04();
	test14();
	test5();
	test6();
	test7();
	test8();
	return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Redis常见应用场景

目录 一、实现博客点赞功能 二、实现博客点赞用户列表功能 三、好友关注和取关以及求共同关注 四、实现关注推送 1、拉模式 2、推模式 3、推拉结合 四、三种模式对比 这里简单记录一下&#xff0c;没有实现方法&#xff0c;只是帮助记忆 一、实现博客点赞功能 可以通…

六、JSON

文章目录 1. 什么是JSON1.1 JSON 在 JavaScript 中的使用1.1.1 json 的定义1.1.2 json 的访问1.1.3 json 的两个常用方法 1.2、JSON 在 java 中的使用1.2.1、javaBean 和 json 的互转1.2.2、List 和 json 的互转1.2.3、map 和 json 的互转 1. 什么是JSON 1.1 JSON 在 JavaScrip…

【数据结构与算法 | 灵神题单 | 自顶向下DFS篇】力扣1022,623

1. 力扣1022&#xff1a;从根到叶的二进制之和 1.1 题目&#xff1a; 给出一棵二叉树&#xff0c;其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。 例如&#xff0c;如果路径为 0 -> 1 -> 1 -> 0 -> 1&#xff0c;那…

Uniapp的alertDialog返回值+async/await处理确定/取消问题

今天在使用uniui的alertDialog时&#xff0c;想添加一个确定/取消的警告框时 发现alertDialog和下面的处理同步进行了&#xff0c;没有等待alaertDialog处理完才进行 查询后发现问题在于 await 关键字虽然被用来等待 alertDialog.value.open() 的完成&#xff0c;但是 alertDi…

Linux操作系统 进程(3)

接上文 Linux进程优先级之后&#xff0c;我们了解到僵尸进程与孤儿进程的形成原因&#xff0c;既然是因为父进程没有接收子进程的退出状态导致的&#xff0c;那么我们该如何去获取子进程的退出状态呢&#xff1f;那本篇文章将围绕这个问题来解释进程。 环境 &#xff1a; vsco…

【C++】——多态详解

目录 1、什么是多态&#xff1f; 2、多态的定义及实现 2.1多态的构成条件 ​2.2多态语法细节处理 2.3协变 2.4析构函数的重写 2.5C11 override 和 final关键字 2.6重载—重写—隐藏的对比分析 3、纯虚函数和抽象类 4、多态的原理分析 4.1多态是如何实现的 4.2虚函数…

光伏场地建设规划 - 华为OD统一考试(E卷)

2024华为OD机试&#xff08;C卷D卷E卷&#xff09;最新题库【超值优惠】Java/Python/C合集 题目描述 祖国西北部有一片大片荒地&#xff0c;其中零星的分布着一些湖泊&#xff0c;保护区&#xff0c;矿区;整体上常年光照良好&#xff0c;但是也有一些地区光照不太好。某电力公…

C++中模板的初级使用函数模板(刚刚接触模板概念的小白也能明白)

文章目录 模板分类函数模板函数模板的原理函数模板基本语法 —— typename 以及 class简单的函数模板多类型模板参数class 和 typename 的选择类模板 模板分类 模板的核心思想是让编译器在编译时生成适用于具体类型的代码&#xff0c;这个过程称为模板实例化。C 中的模板分为两…

Sublime Text 3 相关设置

打开设置 { “font_size”: 16, // 字体大小 “save_on_focus_lost”: true, // 自动保存 }

射击靶标检测系统源码分享

射击靶标检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vis…

【吊打面试官系列-MySQL面试题】LIKE 声明中的%和_是什么意思?

大家好&#xff0c;我是锋哥。今天分享关于【LIKE 声明中的&#xff05;和_是什么意思&#xff1f;】面试题&#xff0c;希望对大家有帮助&#xff1b; LIKE 声明中的&#xff05;和_是什么意思&#xff1f; &#xff05;对应于 0 个或更多字符&#xff0c;_只是 LIKE 语句中的…

Amazon Bedrock 模型微调实践(二):数据准备篇

本博客内容翻译自作者于 2024 年 9 月在亚马逊云科技开发者社区发表的同名博客&#xff1a; “Mastering Amazon Bedrock Custom Models Fine-tuning (Part 2): Data Preparation for Fine-tuning” 亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术文档、…

Leetcode—322. 零钱兑换【中等】(memset(dp,0x3f, sizeof(dp))

2024每日刷题&#xff08;159&#xff09; Leetcode—322. 零钱兑换 算法思想 dp实现代码 class Solution { public:int coinChange(vector<int>& coins, int amount) {int m coins.size();int n amount;int dp[m 1][n 1];memset(dp, 0x3f, sizeof(dp));dp[0][…

Django ORM(多表)

文章目录 前言一、关联关系模型二、一对多写入数据二、多对多写入数据二、跨表查询1.查找test 标签的文章2.查找作者名为 test 的文章及标签 三、跨表删除 前言 表与表之间的关系可分为以下三种&#xff1a; 一对一: 一对一关系表示一个模型的每个实例与另一个模型的每个实例…

【字符函数】strcpy函数(字符串复制函数)+strcat函数(字符串追加)+strcmp函数(字符串比较)【笔记】

1.复制函数--------------strcpy函数 函数使用 char*strcpy&#xff08;char* destination, const char* source&#xff09; strcpy函数用于拷贝字符串&#xff0c;即将一个字符串中的内容拷贝到另一个字符串中&#xff08;会覆盖原字符串内容&#xff09;。它的参数是两个指…

Mysql梳理6——order by排序

目录 6 order by排序 6.1 排序数据 6.2 单列排序 6.3 多行排列 6 order by排序 6.1 排序数据 使用ORDER BY字句排序 ASC&#xff08;ascend&#xff09;:升序DESC(descend):降序 ORDER BY子句在SELECT语句的结尾 6.2 单列排序 如果没有使用排序操作&#xff0c;默认…

【HarmonyOS NEXT】DevEco快速实现真机截屏,并保存到电脑

点日志点照机图标选一个路径保存图片在ide中右键图片&#xff0c;点复制电脑随便找个位置保存图片https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-screenshot-V5

1-2.Jetpack 之 Navigation 跳转编码模板

一、Navigation 1、Navigation 概述 Navigation 是 Jetpack 中的一个重要成员&#xff0c;它主要是结合导航图&#xff08;Navigation Graph&#xff09;来控制和简化 Fragment 之间的导航&#xff0c;即往哪里走&#xff0c;该怎么走 2、Navigate 引入 在模块级 build.gra…

Datawhale------Tiny-universe学习笔记——Qwen(1)

1. Qwen整体介绍 对于一个完全没接触过大模型的小白来说&#xff0c;猛一听这个名字首先会一懵&#xff1a;Qwen是啥。这里首先解答一下这个问题。下面是官网给出介绍&#xff1a;Qwen是阿里巴巴集团Qwen团队研发的大语言模型和大型多模态模型系列。其实随着大模型领域的发展&a…

全同台加密综述

文章目录 一、FHE的定义与性质1、核心算法2、性质 二、构造思想三、全同态加密研究进展1、支持部分同态的 Pre-FHE 方案2、基于理想格的 第1代 FHE方案3、基于LWE的 第2代 FHE方案3、基于近似特征向量的 第3代 FHE方案4、支持浮点数运算的 第4代 FHE方案5、其他 FHE方案5.1、基…