『写在前面的一些话』
上一次整理半天的字符串和数值转换,结果发现PAT的编译器不支持 atoi() 函数。orz
关于字符串由于平时题目用的很多所以整理一下
string是C++标准库的一个重要的部分,主要用于字符串处理。
『一些基本操作』
- substr子串操作
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s="abcdefg";
//s.substr(pos1,n)返回字符串位置为pos1后面的n个字符组成的串,如果输入的位置超过字符的长度,会抛出一个out_of_range的异常
string s2=s.substr(1,5);//bcdef
//s.substr(pos)//得到一个pos到结尾的串
string s3=s.substr(4);//efg
return 0;
}
- insert插入操作:(代码来自cpp)
看了这个代码才知道,string也可以使用迭代器
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);//关闭同步源
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;
//s.insert(pos,str)//在s的pos位置插入str
str.insert(6,str2); // to be the question
//s.insert(pos,str,a,n)在s的pos位置插入str中插入位置a到后面的n个字符
str.insert(6,str3,3,4); // to be not the question
//s.insert(pos,cstr,n)//在pos位置插入cstr字符串从开始到后面的n个字符
str.insert(10,"that is cool",8); // to be not that is the question
//s.insert(pos,cstr)在s的pos位置插入cstr
str.insert(10,"to be "); // to be not to be that is the question
//s.insert(pos,n,ch)在s.pos位置上面插入n个ch
str.insert(15,1,':'); // to be not to be: that is the question
//s.insert(s.it,ch)在s的it指向位置前面插入一个字符ch,返回新插入的位置的迭代器
it = str.insert(str.begin()+5,','); // to be, not to be: that is the question
//s.insert(s.it,n,ch)//在s的it所指向位置的前面插入n个ch
str.insert (str.end(),3,'.'); // to be, not to be: that is the question...
//s.insert(it,str.ita,str.itb)在it所指向的位置的前面插入[ita,itb)的字符串
str.insert (it+2,str3.begin(),str3.begin()+3); // to be, or not to be: that is the question...
return 0;
}
- erase清除操作:(代码来自cpp)
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
//直接指定删除的字符串位置第十个后面的8个字符
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9);// ^
//删除迭代器指向的字符
std::cout << str << '\n';
// "This is a sentence."
// ^^^^^
str.erase (str.begin()+5, str.end()-9);
//删除迭代器范围的字符
std::cout << str << '\n';
// "This sentence."
return 0;
}
-
append添加和replace替换操作:
append函数可以用来在字符串的末尾追加字符和字符串。也可以用+=操作实现
#include <iostream> #include <bits/stdc++.h> #include <string> using namespace std; int main(){ string s = "to be or not to"; string ss = "is a question"; s.append(" be ");//直接追加一个" be "的字符串 // to be or not to be s.append(ss,5,8);//追加ss的第5个字符开始的8个字符 //to be or not to be question //添加5个'.' s.append(5u,'.'); //to be or not to be question..... s.replace(6,2,ss,0,2); //将s的第6个字符后的2个字符替换为0个字符后的2个字符 //to be is not to be question..... return 0; }
-
find和rfind查找函数:
find函数主要是查找一个字符串是否在调用的字符串中出现过,区分大小写。
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
//在str当中查找第一个出现的needle,找到则返回出现的位置,否则返回结尾
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
//在str当中,从第found+1的位置开始查找参数字符串的前6个字符
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
//在str当中查找参数中的字符串
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
//查找一个字符
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
//组合使用,把str2用参数表中的字符串代替
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';
return 0;
}
rfind函数就是找最后一个出现的匹配字符串,返回的位置仍然是从前往后数的。(貌似用kmp实现的)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
std::string str ("The sixth sick sheik's sixth sheep's sick.");
std::string key ("sixth");// ^
//rfind是找最后一个出现的匹配字符串
std::size_t found = str.rfind(key);
if (found!=std::string::npos)
{
cout<<found<<endl;//输出23
str.replace (found,key.length(),"seventh");//找到的sixth替换成seventh
}
std::cout << str << '\n';
return 0;
}
|
扫描二维码关注公众号,回复:
2592720 查看本文章
- compare比较函数
如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。 在compare当中还支持部分比较,里面有6个参数可以设置。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);//消除同步流
string s1="123",s2="123";
cout<<s1.compare(s2)<<endl;//0
s1="123",s2="1234";
cout<<s1.compare(s2)<<endl;//-1
s1="1234",s2="123";
cout<<s1.compare(s2)<<endl;//1
return 0;
}
『感悟』
知道字符串也可以用迭代器,可以消除同步流加速cin。