C++初阶(长期更新)第9讲:string(上)

C++初阶(长期更新)第9讲:string(上)

++跟着潼心走,轻松拿捏C++,困惑通通走,一去不回头~欢迎开始今天的学习内容,++ ++你的支持就是博主最大的动力++ 。博主主页:潼心1412o-CSDN博客

前言

前面学习了一些基础的语法,今天我们正式进入STL的学习,一起学习string的内容,gogogo,出发喽~

8.1 STL

C++标准库提供了许多库,其中STL库中含有许多封装好的数据结构供我们使用

C++ STL的六大组件包括容器、算法、迭代器、函数对象、适配器和分配器,它们协同工作实现高效泛型编程

string就是其中的一个容器

8.2 string

string类文档介绍cplusplus.com/reference/string/string/?kw=string

cpp 复制代码
void Test01()
{
	string s1;
	string s2("Hello world");
	string s3(s2);
	//从下标位置为1开始拷贝6个字符
	//string (const string& str, size_t pos, size_t len = npos);
	//npos == -1,为整型最大值
	string s4(s2, 1, 6);
	//超过字符串长度则拷贝到结束
	string s5(s2, 1, 60);
	//从1位置直接拷贝到结束
	string s6(s2, 1);
	const char* str = "Hello world";
	//拷贝常量字符串的前5个值
	string s7(str, 5);
	string s8(100, '#');

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;
	cout << s7 << endl;
	cout << s8 << endl;
}

8.3 遍历和修改string

这里提供了三种方式:

1.用下标遍历 2.迭代器operator 3.范围for

cpp 复制代码
//遍历加修改
//下标size()==length()
//length()其他类通用
void Test02()
{
	string s2("Hello world");
	for (size_t i = 0; i < s2.size(); i++)
	{
		s2[i]++;
	}
	cout << s2 << endl;
}
//迭代器-可近似理解为指针
//所有容器主流遍历方式
//[begin(),end())左闭右开为指针
void Test03()
{
	string s3("Hello world");
	string::iterator it3 = s3.begin();
	while (it3 != s3.end())
	{
		++(*it3);
		++it3;
	}
	cout << s3 << endl;
}
//范围for 语法糖
void Test04()
{
	string s4("Hello world");

	auto x = 1;
	auto y = 1.0;
    auto p1 = &x;
    auto* p2 = &y;
	int& z = x;
	//这里只是赋值给m,想改变z的值必须加&
	//auto m = z;
	auto& m = z;
	m++;
	cout << x << endl;
	cout << z << endl;
	cout << m << endl;
	//同理要改变s4的值这里auto也必须加&
	for (auto& e : s4)
	{
		++e;
	}
	cout << endl;

	for (auto e : s4)
	{
		cout << e << " ";
	}
	cout << endl;
}

迭代器的意义

1.统一类似的方式遍历和修改容器

2.算法脱离具体底层结构,和底层结构解耦。算法独立模板针对多个容器进行处理

cpp 复制代码
void Test05()
{
	string s = "123";
	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	list<int>lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	list<int>::iterator it5 = lt.begin();
	while (it5 != lt.end())
	{
		cout << *it5 << endl;
		++it5;
	}
	reverse(s.begin(), s.end());
	reverse(v.begin(), v.end());
	reverse(lt.begin(), lt.end());
}

8.4 两道小题

917. 仅仅反转字母 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    bool isLetter(char& ch)
    {
        if(ch>='a'&&ch<='z')
        {
            return true;
        };
        if(ch>='A'&&ch<='Z')
        {
            return true;
        }
        return false;
    }
    string reverseOnlyLetters(string s) 
    {
        int begin=0;
        int end=s.size()-1;
        while(begin<end)
        {
            while(begin<end&&!isLetter(s[begin]))
                ++begin;
            while(begin<end&&!isLetter(s[end]))
                --end;
            swap(s[begin],s[end]);
            ++begin;
            --end;
        }
        return s;
    }

387. 字符串中的第一个唯一字符 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    
    int firstUniqChar(string s) 
    {
        int arr[26]={0};
        for(auto ch : s)
        {
            arr[ch -'a']++;
        }
        for(size_t i=0;i < s.size();i++)
        {
            if(arr[s[i]-'a']==1)
                return i;
        }
        return -1;
    }
};

++好了今天的学习内容就到这里啦,谢谢你的陪伴,我是潼心,下次再见~如果这篇文章对你有帮助的话。请务必给主播一个一键三连,球球了,这对主播很重要~++ ​​

博主主页:潼心1412o-CSDN博客

相关推荐
朝朝辞暮i1 小时前
C++ 第 39 章: 阶段性总复习——类、对象、构造、继承、this、指针与智能指针
开发语言·c++·算法·ros2
wuminyu1 小时前
Virtual Thread重投递至ForkJoinPool任务队列过程解析
java·linux·c语言·jvm·c++
All for pursuit.2 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
掉进电商坑三年没爬出来的东叔2 小时前
青龙面板进阶:一个面板聚合多平台签到,所有积分自动领
java·开发语言
iCxhust2 小时前
51单片机电力载波应用开发(七)广播通讯
c语言·开发语言·单片机·嵌入式硬件·51单片机
Benny_Tang2 小时前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法
殷色玫瑰2 小时前
C++ STL:stack、queue、priority_queue 与容器适配器详解
java·开发语言·数据结构·c++·算法·visualstudio
IT方大同3 小时前
java输入输出+方法
java·开发语言·python
滕州市燕猫虎计算机科技工作室个体工商户3 小时前
《Rust程序设计》学习笔记一
开发语言·rust·学习笔记·rust程序设计