文章目录
============================ 【说明】 ===================================================
大家好,本专栏主要是跟学C++内容,自己学习了这位博主【 AI菌】的【C++21天养成计划】,讲的十分清晰,适合小白,希望给这位博主多点关注、收藏、点赞。
主要针对所学内容,通过自己的理解进行整理,希望大家积极交流、探讨,多给意见。后面也会给大家更新,其他一些知识。若有侵权,联系删除!共同维护网络知识权利!
=======================================================================================
写在前面
至此,我们了解了C++的基本语法,但是进一步学习C++,数据结构是必不可少的内容。 数据结构与算法决定了代码测存储方式,以及代码执行效率。
数据结构的重要性不言而喻, 关于数据结构的基本知识可以转至本人另一专栏====>【 数据结构】。同样也可以阅读博主【 AI菌】写的【 数据结构与算法】,比较通俗易懂,可以学习学习!
1、String类优点
上一节,我们在数组中了解了静态数组和动态数组的区别。在C++中,字符串可以理解为是字符数组,我们可以这样定义:
char staticArray[10]; //声明一个固定长度的静态字符数组
这样一看,我们定义了一个固定长度的静态数组。但是在实际问题中,我们定义的数组长度并不是预先固定好的,同样我们需要声明一个动态字符数组,按照需求可以动态调整数组长度。
在C++中,提供了一个string类,类似vector/deque类,它不仅可以根据程序需求动态调整大小,还提供了很多成员函数,让程序更容易操作,也更方便操作字符串。
2、String类的基本操作
2.1、初始化
String类提供了很多重载的构造函数,我们可以用多种方式进行初始化以及实例化,初始化如下:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//定义string类的六种方式
int main() {
string str1 = "string string";
string str2 = ("string string");
string str3(str1);
string str4(10, 's');
string str5(str1, 5, 5);
string str6(str1.begin(), str1.end());
cout <<"str1:"<< str1 << endl;
cout << "str2:" << str2 << endl;
cout << "str3:" << str3 << endl;
cout << "str4:" << str4 << endl;
cout << "str5:" << str5 << endl;
cout << "str6:" << str6 << endl;
return 0;
}
2.2、访问字符串
字符串需要访问的方法有很多,比如:
(1) 类似数组下标[]
访问方式;
(2) 迭代器
(3) 操作函数at()
具体举例如下:
//string的访问
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main05() {
string str = "string string";
cout << "========Way1:类似数组下标==========" << endl;
for (int i = 0; i < str.length(); i++)
{
cout << "str[<" << i << ">:" << str[i] << endl;
}
cout << "========Way2:迭代器==========" << endl;
int set = 0;
string::const_iterator locator;
for (locator= str.begin(); locator!=str.end(); locator++)
{
cout << "str[<" << set++ << ">" << *locator << endl;
}
cout << "========Way3:操作函数at()==========" << endl;
for (int i = 0; i < str.length(); i++)
{
cout << "str[<" << i << ">:" << str.at(i) << endl;
}
return 0;
}
3、String类的成员函数
String类成员函数有很多,每种成员函数都有它的实际意义。比如计算字符串容量、拼接、新增、删除、反转等。使用这些字符串函数,能够轻松的解决很多字符串操作问题,并且使代码变得更加简洁可读。
此外,还有一些进阶的成员函数,比如查找字符、截短、大小写转换、子字符串、字符串比较等。使用好这些进阶的字符串函数,往往能够题使代码简化,达到半掊的效果!
3.1、容量函数对比
所谓容量,即计算字符数组的长度、最大容量等,常用的函数有:size
、length
、capacity
、empty
。
函数名 | 解释 | 案例 |
---|---|---|
size | 返回字符串长度 | str.size() |
length() | 返回字符串长度 | str.length() |
capacity() | 返回字符串允许存放字符的最大长度(容量) | str.capacity() |
empty() | 判断字符串是否为空,返回类型为布尔值 | str.empty() |
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//string类的基本容量
int main04() {
string str = "string string";
cout << "str's size:" << str.size()<<endl;
cout << "str's length:" << str.length()<<endl;
cout << "str's capacity:" << str.capacity()<<endl;
if (str.empty())
cout << "str is empty" << endl;
else
cout << "str is not empty" << endl;
return 0;
}
3.2、拼接字符串append()
如果想将两个字符串进行拼接,可以使用+=
运算符,也可以用成员函数append()
。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
cout << "========成员函数append()==========" << endl;
string str1 = "study ";
string str2 = "string";
str1 += str2;
cout << str1 << endl;
string str3 = " study!";
str1.append(str3);
cout << str1 << endl;
return 0;
}
3.3、字符串末尾增加字符push_back()
使用push_ back()
我们可以在原字符串末尾新增字符,我们可以将test. txt文件中的字符一个个的写入字符串str。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数push_back()==========" << endl;
string str4 = "study ";
ifstream file("C:/Users/Cqy/Desktop/ConsoleApplication1/ConsoleApplication1/test.txt", ios::in);
while (true){
str4.push_back(file.get());
if (file.eof())
break;
}
cout <<"str4=:"<< str4 << endl;
return 0;
}
注:如果想更为方便在字符串末尾增加元素,同样也可以使用append()
函数,该函数不仅可以增加一个字符,也可以一次增加一个字符串。
3.4、字符串末尾删除字符pop_back()
使用pop_ back()
函数可以在原字符串后删除一个字符,这与前面的list类和vector类中的用法也是一样的。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数pop_back()==========" << endl;
string str5("Good night!");
str5.pop_back();
cout << "str5=" << str5 << endl;
return 0;
}
3.5、字符串反转reverse()
字符串反转就是字符串的字符首尾倒序存放。比较熟悉的一个案例就是判断某字符串是否是回文串,就可以将其反转,再与原来的字符串进行比较。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数reverse()==========" << endl;
string str6("Good night!");
reverse(str6.begin(), str6.end());
cout << "str6=" << str6 << endl;
return 0;
}
3.6、查找字符或子字符串find()
在string类中,封装了用来查找字符串中的字符或者子字符串。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
cout << "========成员函数find()==========" << endl;
string str7("I love you! and do you love me?");
cout << "str7:" << str7 << endl;
//1.从最开始处开始搜索,返回字符串love第一次出现的位置
int position = str7.find("love", 0);
if (position != string::npos) //string:npos实际值为-1,表示没有找到要搜索的元素
cout << "字符串love第一次出现的位置:" << position << endl;
else
cout << "字符串love没有被找到" << endl;
//2.返回所有子字符串的位置
cout << "字符串love出现的所有位置:";
int all_pos = str7.find("love", 0);
while (all_pos != string::npos)
{
cout << all_pos << " ";
int search_pos = all_pos + 1;
all_pos = str7.find("love", search_pos); //返回子字符串出现的位置
}
return 0;
}
注:在string中的find()
函数和algorithm中的find()
函数是有区别的。
string中的find()
函数是从位置0开始寻找,寻找str字符串中第一次出现字串substr的位置。
int pos = str.find(substr, 0);
algorithm中的find()
函数也是从位置0开始寻找,寻找str字符串中第一次出现字串substr的位置,返回的是迭代器。
string::iterator pos = find(str.begin(),str.end(), substr);
3.7、截短字符串erase()
截短字符串函数erase()
有三种用法:
(1) 给定偏移位置(删除的起始位置)和要删除的字符个数;
(2) 在给定指向字符的迭代器时删除该字符;
(3) 在给定两个迭代器指定的范围时,删除该范围内的字符;
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout <<endl;
cout << "========成员函数erase()==========" << endl;
cout << " ======Way1=======" << endl;
string str8 = "I love you very much!";
str8.erase(2, 4); //从索引号2开始,删除4个字符,即删掉love。
cout << str8 << endl;
cout << " ======Way2=======" << endl;
string str9("I love you! and do you love me?");
string::iterator pos = find(str9.begin(), str9.end(), 'I'); //找到字符'I'的位置给迭代器
if (pos != str9.end())
str9.erase(pos); //依次删除迭代器指向的字符
cout << str9 << endl;
cout << " ======Way3=======" << endl;
//str9.erase(str9.begin(), str9.end());
return 0;
}
3.8、大小写转换transform()
使用大小写转换函数transform()
,可以实现字符串的大小写转换。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数transform()==========" << endl;
string str10("sTriNG");
//1.将字符串s1转换为大写
transform(str10.begin(), str10.end(), str10.begin(), toupper);
//2.将字符串s1转化为小写
transform(str10.begin(), str10.end(), str10.begin(), tolower);
return 0;
}
3.9、获取子字符串substr()
使用substr()
,可以方便地获取字符串的任意子串。声明如下:
string substr (size_t pos = 0, size_t len = npos)
第一个参数是,待获取的子串的起始位置;默认为0,即首字符。
第二个参数是,待获取子串的长度;默认npos, 即一直取到末尾。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数substr()==========" << endl;
string str11 = "We think in generalities, but we live in details.";
string str12 = str11.substr(3, 5); //str2 = think
rsize_t index = str11.find("live"); // position of "live" in str
string str13 = str11.substr(index); // get from "live" to the end
cout << str12 << ' ' << str13 << endl;
return 0;
}
3.10、字符串比较compare()
使用compare()
,可以两个字符串进行比较。以str1.compare(str2)
为例,比较规则如下表所示:
返回值 | 解释 |
---|---|
=0 | str1=str2 |
<0 | str1的第一个不匹配的字符的值较小,或者所有比较的字符都匹配,但str1比str2短 |
>0 | str1的第一个不匹配的字符的值较大,或者所有比较的字符都匹配,但str1比str2长 |
注:对于这个表格,可以这样简单理解,字符串大小此较规则就是:从左往右,依次比较每个字符对应的ASCII码值,大者为大,小者为小;若相等,则继续比较后面的字符。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
cout << "========成员函数compare()==========" << endl;
string str1 = "Hello World!";
string str2 = "Hello String!";
//用法1:比较str1与str2
if (str1.compare(str2) == 0)
cout << "str1=str2" << endl;
else if (str1.compare(str2)<0)
cout << "str1<str2" << endl;
else
cout << "str1>str2" << endl;
//用法2:取str1的第6个位置后的5个字符World与字符串World比较
if (str1.compare(6, 5, "World") == 0)
cout << "相等" << endl;
else
cout << "不等" << endl;
//用法3:截取两个字符串各自的子串进行比较
if (str1.compare(0, 5, str2, 0, 5) == 0)
cout << "str1的子串Hello = str2的子串Hello" << endl;
else
cout << "str1的子串Hello != str2的子串Hello" << endl;
return 0;
}
4、总结
最后,长话短说,大家看完就好好动手实践一下,切记不能三分钟热度、三天打鱼,两天晒网。大家也可以自己尝试写写博客,来记录大家平时学习的进度,可以和网上众多学者一起交流、探讨,我也会及时更新,来督促自己学习进度。一开始提及的博主【AI菌】,个人已关注,并订阅了相关专栏(对我有帮助的),希望大家觉得不错的可以点赞、关注、收藏。