记录字符串常考的4种操作:遍历、加密、统计、匹配。
1.字符串介绍
C++提供了字符串(string)这种基本数据类型,它可以很方便地对字符串进行各种操作。使用需要添加头文件#include<string>
。
1.1字符串的构造
字符串的构造包括定义和初始化两个部分,定义一个字符串的方式和定义其他基本数据类型的方式相同,如string str;
,此外还有很多种常用的定义方式,可根据题面需要进行选择。
字符串定义
//常见的定义字符串的一些方法 2024-02-17
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string s0 = "Initial String"; //初始化可以直接给字符串变量赋值
string s1;
string s2 (s0); //把s0的拷贝给s2
string s3 (s0, 8, 3); //从字符串s0的下标8开始取3个字符拷贝给s3(下标从0开始) Str
string s4 ("A character sequence"); //用构造函数的方式给s4值
string s5 ("Another character sequence", 12); //Another char 只需要前面12个字符
string s6 (10, 'x'); //假如你现在要建立一个字符串,这个字符串由n个重复的字符构成,使用这种方式 10个x
cout << "s0:" << s0 <<endl;
cout << "s1:" << s1 <<endl;
cout << "s2:" << s2 <<endl;
cout << "s3:" << s3 <<endl;
cout << "s4:" << s4 <<endl;
cout << "s5:" << s5 <<endl;
cout << "s6:" << s6 <<endl;
return 0;
}
1.2字符串的操作
1、对于操作来说,最重要的就是访问,要访问到字符串的每一位字符。字符串类型已经重载了[]
,让我们能够像访问数组元素一样用下标访问字符串的每一位。
2、第二种操作是插入,可以在字符串的任意位置插入一段字符串或者插入字符。调用str4.insert(pos1,str, pos2, n)
函数,其中str4是我们定义的变量名字,有4个参数,第一个参数指明从字符串str4的哪个下标位置开始插入;第二个参数指明插入的字符串内容,可以是字符串变量str2,也可以直接显示给出;第三个参数指明从str2的哪个下标位置开始取字符;第四个参数配合前一个参数指明取字符的个数。
3、移除操作。只有两种方式,1:从字符串的某个下标位置开始截断,后面都不要了。2:只删一小部分,第一个参数还是指明从哪个开始删;第二个参数指明一共删除几个字符。str4.erase(pos, n);
4、清空操作。直接调用str4.clear()
,清空字符串。
字符串的操作
//字符串的元素操作 2024-02-17
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str = "hello world";
for(int i = 0; i < str.size(); ++i) {
cout << str[i];
}
cout << endl << "the 7th element of str is: " << str[6] << endl;
string str1 = "to be question";
string str2 = "that is a ";
string str3 = "or not world";
string str4;
str4.insert(0, str1); //在str4下标为0的位置插入str1 //to be question
str4.insert(6, str3, 0, 7); //在str4下标为6的位置插入,只插入str3的一部分,从0的位置开始,插7个字符 //to be (or not )question
str4.insert(13, "to be "); //也可以显示的、把要插入的字符串插入进来//to be or not (to be )question
str4.insert(19, str2); //to be or not to be (that is a )question
cout << "str4: " << str4 << endl;
str4.erase(18); //从下标哪个位置开始删除
str4.erase(0, 9);
cout << "str4: " << str4 << endl;
str4.clear();
cout << "str4: " << str4 << endl;
return 0;
}
1.3字符串的运算
字符串的运算与数组不同,字符串的加法变成了字符串的拼接;而> < = != >= <=
这一类的运算符也被重载,用字典排序进行大小比较(原本字符串是没有大小之分的)。
字符串的运算符
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str1 = "to be ";
string str2 = "nor not to be ";
string str3 = "that is a question";
string str = str1 + ",";
str = str + str2 + ";";
str += str3;
cout << str << endl;
str1 = "ab";
str2 = "abc";
str3 = "bcc";
cout << (str1 <= str2) << endl;
cout << (str2 != str3) << endl;
cout << (str3 < str1) << endl;
return 0;
}
1.4字符串的常用函数
1、返回字符串长度的函数:str1.size()
。
2、在字符串里查找一小段字符串或者字符:str1.find("xxx")
,会返回第一个等于这个小字符串的位置下标。或者可以指定从哪个位置开始找,默认从字符串的首部开始找。如果找得到就返回下标,找不到就返回-1。
3、返回字符串的子串的函数:str1.substr(pos, n);
有两种方式,1:给定一个下标,从这个下标开始后面所有的字符一并返回给你;2:只要某一小段,给定参数1从哪里开始,参数2总共截取几个字符。
字符串的运算符
//字符串的常用函数 2024-02-17
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str1 = "bella, Things will just be fine";
cout << str1.size() << endl; //字符个数
int position1 = str1.find("be");
int position2 = str1.find("be", 2);
cout << "position1: " << position1 << endl;
cout << "position2: " << position2 << endl;
//也可以找单个字符
position1 = str1.find('l');
position2 = str1.find('l', 5);
cout << "position1: " << position1 << endl;
cout << "position2: " << position2 << endl;
string str2 = str1.substr(7); //从下标为7的位置开始取子串
cout << "str2: " << str2 << endl;
string str3 = str1.substr(7,6); //Things
cout << "str3: " << str3 << endl;
return 0;
}