1.为什么要学习string类
在此我们先聊一聊各个string类和编码
1.1C语言中的字符串
C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,
但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可
能还会越界访问。
在OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本
都使用string类,很少有人去使用C库中的字符串操作函数。
2.string类的使用
强烈建议大家通过文档学习查阅
文档链接:C++ string类
npos
operator= / operator[ ]
at
练习:仅仅反转字母
string的迭代器
push_back / append / operator+=
string类的增容规律(reserve)
resize
assign:赋值
assign:赋值 不常用
功能:为字符串赋值,替代当前字符串
void test_string9()
{
string s("hello");
s.assign("nongda");
cout << s << endl;
}
insert:插入
功能:可以在任意位置插入字符或字符串
//要求:在每个空格处加一个20%
//1.简单易看版
void test_string11()
{
string s("wo lai le!!!");
for (size_t i = 0; i < s.size();)
{
if (s[i] == ' ')
{
s.insert(i, "20%");
i += 4;//当在空格前加了一个20%时,需要控制i跳过此空格
//去找下一个空格,否则会死循环
}
else
{
++i;
}
}
cout << s << endl;
}
//2.简洁版
void test_string10()
{
string s("wo lai le!!!");
for (size_t i = 0; i < s.size(); i++)
{
if (s[i] == ' ')
{
s.insert(i, "20%");
i += 3;//当在空格前加了一个20%时,需要控制i跳过此空格
//去找下一个空格,否则会死循环
}
}
cout << s << endl;
}
//上述代码用了insert,它的时间复杂度为O(n^2),效率较低
//如何改进提高程序效率呢?
//方法:以空间换时间
void test_string12()
{
string s("wo lai le!!!");
string newstr;
for (size_t i = 0; i < s.size(); i++)
{
if (s[i] != ' ')
{
newstr += s[i];
}
else
{
newstr += "20%";
newstr += ' ';
}
}
cout << newstr << endl;
}
erase:删除
void test_string13()
{
string s("wo lai le!!!");
for (size_t i = 0; i < s.size(); i++)
{
if (s[i] == ' ')
{
s.erase(i, 1);
}
}
cout << s << endl;
}
replace:替换
replace替换,需要挪动数据,效率较低,一般不建议使用
//将空格处替换成"20%"
void test_string12()
{
//string s("wo lai le!!!");
//for (size_t i = 0; i < s.size(); i++)
//{
// if (s[i] == ' ')
// {
// //replace的效率较低,不建议使用,当然看情况
// s.replace(i, 1, "20%");
// 将i位置的一个值替换成"20%"
// }
//}
//cout << s << endl;
//改进,空间换时间
string s("wo lai le!!!");
string newstr;
for (size_t i = 0; i < s.size(); i++)
{
if (s[i] != ' ')
{
newstr += s[i];
}
else
{
newstr += "20%";
}
}
cout << newstr << endl;
}
c_str:获取等价的C字符串
find:查找
substr:得到一个字符串的子串
void test_string16()
{
//如何得到s的子串.cpp
string s("test.cpp");
size_t pos = s.find('.');
if (pos != string::npos)
{
//string suff = s.substr(pos, s.size() - pos);
string suff = s.substr(pos);
cout << suff << endl;//.cpp
}
//?
//cout << suff << endl;
}
void Dealurl(string& url)
{
size_t pos1 = url.find(':');
if (pos1 == string::npos)
{
cout << "非法url" << endl;
return;
}
string protocol = url.substr(0, pos1);
cout << protocol << endl;
size_t pos2 = url.find('/', pos1 + 3);
if (pos2 == string::npos)
{
cout << "非法url" << endl;
return;
}
string domain = url.substr(pos1 + 3, pos2 - pos1 - 3);
cout << domain << endl;
string uri = url.substr(pos2 + 1);
cout << uri << endl;
//cout << url << endl;
}
void test_string17()
{
//如何将一网址的协议,域名和路径分开
string url1 = "https://cplusplus.com/reference/string/string/rfind/";
string url2 = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=15007414_12_dg&wd=csdn&fenlei=256&rsv_pq=abcf16da00043980&rsv_t=ddc6694UIUlnUp%2BZDIZRD3Fx%2FFOaoJ%2B%2Br0o3P8kWMh7C8UAAehX6JkX7jHha8P98wFUqi2E&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=csdn&rsp=5&inputT=1174&rsv_sug4=1173&rsv_sug=1";
string url3 = "https://editor.csdn.net/md?articleId=126598608";
Dealurl(url1);
Dealurl(url2);
Dealurl(url3);
}
find_first_of / find_last_of
find_first_not_of / find_last_not_of
不清楚的话建议查阅文档
C++文档