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

news2024/9/20 0:40:05

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.coloradmin.cn/o/2147415.html

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

相关文章

python脚本编译为.so速度对比

有两个好处&#xff1a; 产品代码保护&#xff0c;so文件不可读 计算能力加速&#xff0c;本质上编译过程为python -> c -> so文件&#xff0c;相当于动态语言转换为静态语言&#xff0c;程序执行能力和计算能力有所提升 编译为so文件后比原始python代码执行时间快2ms左…

VISIA 皮肤检测

费用:自费158元 不能医保报销 先清洁肌肤,然后做一个皮肤检测. 1200万像素高清摄像头,一个白光,一个偏正光,还有一个紫外光,三种模式,分析面部情况. 8张图 反应皮肤情况应用: 在医美前和医美一次修复完成后,皮肤情况对比. 数值越高 越好 斑点图: 皱纹图: 分数比较低的话,皮肤…

【计算机基础题目】二叉树的前序中序后续遍历之间相互转换 详细例子

创作日志&#xff1a; 笔试题目&#xff0c;掌握了技巧之后这道题就是 so easy~ 一、 1、已知二叉树的 前序和中序&#xff0c;可以求出后序 2、已知二叉树的 中序和后序&#xff0c;可以求出前序 3、已知二叉树的 前序和后序&#xff0c;无法求出唯一的中序 二、求法 求法是…

828华为云征文|华为云Flexus云服务器X实例之openEuler系统部署Docker Compose管理工具Dockge

828华为云征文&#xff5c;华为云Flexus云服务器X实例之openEuler系统部署Docker Compose管理工具Dockge 前言一、Flexus云服务器X实例介绍1.1 Flexus云服务器X实例简介1.2 Flexus云服务器X实例特点1.3 Flexus云服务器X实例使用场景 二、Dockge介绍2.1 Dockge简介2.2 Dockge功能…

华为云DevSecOps和DevOps

目录 1.华为云DevSecOps和DevOps 1.1 DevSecOps 1.1.1 核心功能 1.1.2 优势 1.2 DevOps 1.2.1 核心功能 1.2.2 优势 1.3 DevOps和DevSecOps的区别 1.3.1 安全性集成 1.3.2 自动化的安全工具 1.3.3 团队协作 1.3.4 质量与合规性 1.3.5 成本与风险管理 1.3.5 总结 …

添可2024新品发布会,让智能家电成为“美好家”的具象表达

9月19日&#xff0c;添可以“万物新生&#xff0c;智领美好家”为主题&#xff0c;于上海浦东美术馆举办2024年度新品发布会。 会上&#xff0c;添可凭借对智能家电与家居设计领域的深刻洞察&#xff0c;全新发布了芙万Art Station智能洗地机、芙万Artist洗地机、饮万水纪元净…

《拿下奇怪的前端报错》:npm install卡住了一个钟- 从原理搞定安装的全链路问题

相信前端的小伙伴稍微入行一段时间的&#xff0c;接触过不同的项目&#xff0c;就可能遇到过npm install卡住的情况&#xff0c;下面我就来分析下几种场景、定位的方法和解决办法。不再只是删除node_modules然后重来了&#xff0c;虽然它能解决80%的问题 1 npm install 的原理…

Vue 常用高级指令解析

Vue 高级指令的重要性 Vue 高级指令是一种扩展 Vue.js 框架的功能的方式&#xff0c;可以让你在处理 DOM 元素时具有更多的控制权。它们可以通过自定义指令的方式进行编写和应用。 高级指令的重要性在于&#xff0c;它们使开发者能够通过 Vue 框架来创建更加复杂和灵活的交互…

数据库函数

1.字符串函数 例子&#xff1a; 2.数值函数 例子&#xff1a; 3.日期函数 例子&#xff1a; 4.流程函数 例子&#xff1a; 参考视频&#xff1a;27. 基础-函数-字符串函数_哔哩哔哩_bilibili

FinGPT金融大模型

FinGPT仓库https://github.com/AI4Finance-Foundation/FinGPT 功能&#xff1a; Adviser。根据新闻判断市场情绪&#xff08;积极、消极、中性&#xff09;&#xff0c;给出投资建议。Quantitative Trading。定制属于自己的金融助手。叫它关注某几个股票、监测消息等。可以直…

Linux安装、Nginx反向代理、负载均衡学习

系列文章目录 第一章 基础知识、数据类型学习 第二章 万年历项目 第三章 代码逻辑训练习题 第四章 方法、数组学习 第五章 图书管理系统项目 第六章 面向对象编程&#xff1a;封装、继承、多态学习 第七章 封装继承多态习题 第八章 常用类、包装类、异常处理机制学习 第九章 集…

代码管理-使用TortoiseGit同步项目到Github/Gitee

1 什么是TortoiseGit TortoiseGit下载地址 TortoiseGit是Git的Windows桌面可视化工具&#xff0c;通过软件的操作来实现Git命令的效果&#xff0c;使所有的操作都能用图形化实现。TortoiseGit安装很简单&#xff0c;这里不对安装流程进行讲解。下载之后即可按照普通软件的方式…

无人机之AI跟踪篇

无人机的AI识别技术依托于计算机视觉和深度学习技术&#xff0c;实现了对目标的快速精准识别&#xff0c;在多个领域展现出了巨大的应用潜力和价值。以下是对无人机AI识别技术的详细解析&#xff1a; 一、无人机AI识别算法的基础原理 无人机AI识别算法主要基于先进的计算机视觉…

使用FLBOOK快速制作3D电子版翻页产品册

​随着数字化时代的到来&#xff0c;传统纸质产品册已逐渐无法满足人们快节奏、便捷的生活方式。而FLBOOK&#xff0c;一款强大的3D电子版翻页产品册制作工具&#xff0c;凭借其简洁的操作界面、丰富的功能和出色的展示效果&#xff0c;已成为越来越多企业的首选。 1.要制作电子…

Ceph官方文档_01_Ceph简介

目录 Ceph介绍Ceph介绍 Ceph可用于向云平台提供Ceph对象存储,Ceph可用于向云平台提供Ceph块设备服务。Ceph可用于部署Ceph文件系统。所有Ceph存储群集部署开始都是先设置每个Ceph节点,然后再设置网络。 Ceph存储集群需要以下内容:至少一个Ceph监视器和至少一个Ceph管理器,…

DA14531开发板原理图设计

一、TYPE-C接口: 二、基于CP2102的USB转UART: 三、UART串口电平转换&#xff1a; 四、扩展接口部分&#xff1a; 五、DA14531蓝牙部分&#xff1a;

【纯小白论文代码带读】医学图像分割MASDF-Net(问题产生及解决)

论文链接&#xff1a;https://www.semanticscholar.org/paper/MASDF-Net%3A-A-Multi-Attention-Codec-Network-with-and-Fu-Deng/6ab609eb93dfd12596032174ca9603712f5c050a 代码链接&#xff1a;https://github.com/Rayicer/TransFuse 初见面代码&#xff1a; Q&am…

多路径文件批量下载工具V1.0.3-支持批量下载文件到单独文件夹的工具-供大家学习研究参考

1、支持批量下载列表文件中的所有文件到每个文件指定的目录下。 2、支持TXT文件导入。 3、支持TXT文件拖入。 4、支持下载错误文件筛选导出。 5、支持单文件多线程下载。 6、其它功能还在开发中。 7、支持断点续传。 8、支持递归下载&#xff08;递归下载就是按照服务器目录结构…

【资料分析】刷题日记2

第一套 √ 2013-2016一共有13&#xff0c;14&#xff0c;15&#xff0c;16四年&#xff0c;亦即16 - 13 1 4年 √ 是多少倍 ③vs④&#xff1a;都是只给出了年均增速&#xff0c;③求的是其中一年的&#xff0c;无法确定&#xff1b;④求的是这个时段总共的&#xff0c;可…

web - JavaScript

JavaScript 1&#xff0c;JavaScript简介 JavaScript 是一门跨平台、面向对象的脚本语言&#xff0c;而Java语言也是跨平台的、面向对象的语言&#xff0c;只不过Java是编译语言&#xff0c;是需要编译成字节码文件才能运行的&#xff1b;JavaScript是脚本语言&#xff0c;不…