strstr 判断字符串中我是不是你的子串,用来查找子字符串的
在str1字符串中查找str2字符串第一次出现的位置,如果找到了,就返回第一次出现的地址,如果没有找到子串,就返回NULL(0)
char arr1[] = "abcdef";
char arr2[] = "def";
if(strstr(arr1,arr2)) 表示在arr1中找arr2的地址
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
string 字符串 需要包含头文件 #include<string>
string s1; 空字符串
string s2 = "abc"; 字符串的初始化,不在以\\0作为结束标志
string s3("hello world"); s3的初始化
cout << s3 << endl;
string s4 = s3; s4可以通过s3来初始化
cout << s4 << endl;
string s1("hello world");
string s2 = "hehe";
s2 = s1; 在c++中使用string是可以直接将s1的值赋值给s2的
cout << s2 << endl;
string s;
cin >> s; cin在遇到空格的时候会停止
cout << s << endl;
getline是c++中的输入流中读取一行文本,并将其储存为字符串
istream 是输入流类型,cin是istream类型的标准输入流对象
ostream 是输出流类型,cout是ostream类型的标准输出流对象
getline函数是以(‘\\n’)作为字符串的结束标志的
string s;
getline(cin,s); 可以使用getline来解决cin输入时遇到空格就停下的问题
cout << s << endl;
getline 函数是允许用户自定义结束标志的
getline(cin,string str,char delim); 自定义的结束标志为delim,遇到delim结束,存储到指定的string类型的变量 str中去
string s;
getline(cin,s,'q');输入字符串为s ,自定义遇到q停止
cout << s << endl;
.size() 计算字符串的长度
string s1;
string s2 = "hello world";
string s3 = "abce #$%";
cout << s1.size() << endl;
cout << s2.size() << endl;
cout << s3.size() << endl;
iterator 迭代器
迭代器的作用类似于指针或者数组下标,访问迭代器指向的值需要解引用(*)
begin() 返回指向字符串第一个字符的迭代器,需要一个迭代器的变量来接收
end()返回指向字符串最后一个字符的下一个位置的迭代器,该位置不属于字符串
在string中begin()和end()返回的迭代器的类型是string::iterator
迭代器是可以比较大小的,也可以进行+/-的整数运算。同一个容器的两个迭代器也可以相减,相减结果的绝对值是两个迭代器中间的元素个数
迭代器的大比较
string s = "abcdef";
string::iterator it1 = s.begin();
string::iterator it2 = s.endl();
if(it1 < it2)
cout << "<" << endl;
else
cout << ">=" << endl;
string s = "abcdef";
string::iterator it1 = s.begin();
cout << *it1 << endl; //解引用操作
迭代器通常用于遍历字符串,可以正序遍历,也可以倒序遍历
string s = "abcdef"; 正序遍历举例
for(string::iterator it = s.begin();it < s.end(); i++)
{
cout << *it;
}
string s = "abcdef"; 倒序遍历
for(string::iterator it = s.end() – 1; it >= s.begin(); it–)
{
cout << *it;
}
push_back() 用于在字符串尾部插入一个字符
string s;
s.push_back('h');
s.push_back('e');
cout << s << endl;
string类型的字符串是支持+和+=运算的,就是在字符串的后面加上字符串,string中重载了operator这个操作符。
string s = "hello";
s += "world";
cout << s << endl;
string s = "hello";
s = "world" + s; world加在hello的前面
cout << s << endl;
pop_back() 用于删除字符串中尾部的一个字符
string s = "hello";
s.poop_back();
cout << s << endl;
insert 在字符串中间的某个位置插入一个字符串
string s = "abcdefghi";
string str = "xxx";
cout << s << endl;
s.insert(3,str); 表示在下标为3的字母前面插入str类型的3个XXX
cout << s << endl;
string s = "abcdefgi";
cout << s << endl;
s.insert(3,1,'x'); 表示在下标为3的字母前面插入1个X
cout << s << endl;
find()函数用于查找字符串中指定子串/字符串,并返回子串/字符串中第一次出现的位置
string s = "hello wold hello everyone"; 第一种写法
string str = "llo";
size_t n = s.find(str); 默认是从头开始查找的
cout << n << endl;
n = s.find(str,n+1); 可以指定查找位置
cout << n << endl;
string s = "hello world hello everyone";
size_t n = s.find("llo"); 第二种写法
cout << n << endl;
n = s.find("llo",n+1);
cout << n << endl;
string s = "hello world hello everyone"; 第三种写法
size_t n = s.find("world",0,3); 从world的第一个字母开始向后查找3个
cout << n << endl;
n = s.find("everyone",n+1,3); 从everyone的n+1个字母开始向后找3个
cout << n << endl;
string s = "hello world hello everyone";
size_t n = s.find('o'); 找到第一个o就会返回打印
cout << n << endl;
n = s.find('o',n+1);
cout << n << endl;
find函数返回npos这个值,该数字不是随机值,而是string中定义的一个静态常量npos,通常判断find函数的返回值是否等于npos来判断查找是否成功。
string s = "hello world hello everyone";
size_t n = s.find("bit");
if(n == string::npos)
cout << "找不到" << endl;
else
cout << "找到了,位置是:" << n << endl;
substr()函数用于截取字符串中指定位置指定长度的子串
substr()如果函数不传参,就从下标为0的位置开始截取,直到结尾,得到的是整个字符串
substr(pos)从指定下标pos位置开始截取子串,直到结尾
substr(pos,len)从指定下标pos位置开始截取长度为len的子串
string s = "hello world hello everyone";
string s1 = s.substr();
cout << s1 << endl;
string s2 = s.substr(7); 表示从下标为7的字符开始截取
cout << s2 << endl;
string s3 = s.substr(7,6); 表示从下标为7的字符开始往后截取6个字符
cout << s3 << endl;
string中,字符串的比较是基于字典序进行的,比较是对应位置上字符的ascll值的大小,比较的不是字符串的长度
和 string相关的函数
stoi 是将字符串转换成int类型
stol是将字符串转换成long int类型的值
语法形式 int stoi(const string str,size_t* idx = 0, int base = 10);
语法形式的解释:str表示被转换的string类型的字符串。idx 是一个输出型参数,通过这个参数会带回一个值,idx是一个指针,需要在外边创建一个size_t类型的值,传递他的地址给idx,这个参数会带回str中无法正确匹配数字的第一个字符的位置。base表示被解析的字符串中数字的进制,默认情况下这个值是10进制。
int main()
{
int stoi(const string str,size_t* idx = 0, int base = 10);
string s = "11×22";
size_t pos = 0;
int r = stoi(s,&pos,2) ; 认为s中的数字是2进制
cout << r << endl;
cout << "pos = " << pos << endl;
return 0;
}
stod 是将字符串转换成double类型的值
stof是将字符串转换成float类型的值
对于浮点型来说不存在进制一说
string s = "3.14abc234";
size_t pos;
double d = stod(s,&pos);
cout << d << endl;
cout << "pos= " << pos << endl;
to_string 需要头文件#include<string>,函数可以将数字转换成字符串,也就是将整形、浮点型的数字转换成字符串的
string s = to_string(110);
cout << s << endl;
isspace 专门用来判断一个字符是否为空白字符的,需要的头文件为#include<cctype>
reverse 可以完成字符串的逆序(反转),需要头文件为 #include<algorithm>
string s = "abcdef";
reverse(s.begin(),s.end());
逆序范围是 first 到 last 内的元素
cout << s << endl;
first 指向要反转范围的第一个元素的迭代器(也可以是地址)
last 指向要反转范围的最后一个元素的下一个位置的迭代器(也可以是地址)(反转时不包括此元素)



