ヾ(๑╹◡╹)ノ" 人总要为过去的懒惰而付出代价ヾ(๑╹◡╹)ノ"
STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
STL六大组件
开源的:linux、git、STL、mysql、安卓……【开源会发展很快】
容器:存储数据(数据结构)
string文档链接
在使用string类时,必须包含#include头文件以及using namespace std;或者using std::string(头文件:#include < string > :这里加不加.h都是可以的,但是尽可能还是不加【为了和C语言进行区分】)
编码:
Unicode:针对全世界编码
gbk:针对中文的编码
ascii:针对英文编码
'A’内存中存储的是65
char类型,所以打印出来的是‘A’.
遗落小知识点:
- 一个汉字:两个字节【一般情况下】
string构造文档
string可以看做管理动态增长的字符数组【需要兼容C++】,字符串以’\0’结尾
string的构造函数是非常多的,在这里我们仅仅掌握常见的即可。【其他的构造我们可以通过查阅文档来解决】
常用的有:无参的构造函数、带参的常量构造、拷贝构造
代码展示:
int main()
{
string s;//创建空字符串;无参的构造函数
string s2("abcd");//常量字符串初始化//带参数的构造函数
s2 += "efg";
cout << s2 << endl;
string s3 = s2;//拷贝构造的两种写法
string s4(s3);
cout << s3 << endl;
cout << s4 << endl;
//以上常见
string s5("abcdefg", 3);//取一段字符串的前3个进行构造
cout << s5 << endl;
string s7(5, 'b');//5个b进行构造
cout << s7 << endl;
string s8("abcdefg", 3, 3);//从第三个开始,去三个字符进行构造
cout << s8 << endl;
return 0;
}
iterator迭代器
element access 元素访问
迭代器:std::iterator【迭代器理解:迭代器像指针一样】
int main()
{
string s1("hello");
const string s2("hello");
s1[0] = 'x';
s2[0] = 'x';//s2不可写,所以报错,const对象不可修改
}
- [ ]用的比较频繁,at不经常用
- [ ]如果出现越界问题,会出现断言错误【assert】;at出现越界会抛异常。
- front 和 back是返回第一个和最后一个有效字符
迭代器分为四种:正向迭代器、反向迭代器(reverse)【这两种可读可写】、const正向迭代器、const反向迭代器
int main()
{
//正向迭代器
string s("hello");//可读可写
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//反向迭代器
string::reverse_iterator rit = s.rbegin();//注意类型
while (rit != s.rend())
{
cout << *rit << " ";
++rit;//注意,这里是++,不是--
}
cout << endl;
可读不可写
可读不可写
//const正向迭代器
const string s1("abcdefg");//可读不可写
string::const_iterator cit = s1.begin();
while (cit != s1.end())
{
cout << *cit << " ";
++cit;
}
cout << endl;
//const反向迭代器
string::const_reverse_iterator crit = s1.rbegin();//注意类型
while (crit != s1.rend())
{
cout << *crit << " ";
++crit;//注意,这里是++,不是--
}
cout << endl;
return 0;
}
遍历字符串
//遍历字符串
int main()
{
string s1("abcdefg");
//第一种方法:下标+[]//[]这个操作符已经重载//类比数组
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << ' ';//[]相当于调用s1.operator[](i)
//注意和内置类型的区别
}
cout << endl;
//第二种方法:迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << ' ';
it++;
}
cout << endl;
//第三种方法:范围for循环//C++11 原理上还是迭代器
for (auto ch : s1)
{
cout << ch << ' ';
}
cout << endl;
}
- s.size() 不包含’\0’
- begin指向第一个有效数据的位置,end指向有效数据的下一个位置【大部分情况下是’\0’】
注意:
swap
int main()
{
string s1("hello");
string s2("abcde");
s1.swap(s2);//效率高 s1和s2的指针进行交换
swap(s1, s2);//效率低 深拷贝交换
return 0;
}
c_str
string s1("hello");
string s2("abcde");
cout << s1.c_str() << endl;
//打印结果 hello
find+substr+rfind
string s1("hello");
cout << s1.find('l') << endl;//返回下标
cout << s1.find('m') << endl;//返回值为size_t,没有找到返回npos(-1),因为是size_t
注意:
取文件的后缀:代码展示
int main()
{
//find substr
string file("test.cpp");
size_t pos = file.find('.');
if (pos != string::npos)
{
string suf = file.substr(pos, file.size() - pos);
cout << file << "后缀是:" << suf << endl;
}
else
{
cout << "没有后缀" << endl;
}
//rfind+substr
string file1("test.cpp.zip");
size_t pos1 = file1.rfind('.');
if (pos1 != string::npos)
{
string suf1 = file1.substr(pos1, file1.size() - pos1);
cout << file1 << "后缀是:" << suf1 << endl;
}
else
{
cout << "没有后缀" << endl;
}
return 0;
}
int main()
{
string url1("https://legacy.cplusplus.com/reference/string/string/?kw=string");
string protocol;//协议
size_t pos1 = url1.find("://");//返回的是字符串的起始下标
if (pos1 != string::npos)
{
protocol = url1.substr(0, pos1);
cout << "protocol: " << protocol << endl;
}
else
{
cout << "非法url" << endl;
}
string domain;//域名
size_t pos2 = url1.find('/', pos1 + 3);//从pos1+3这个位置开始搜索
if (pos2 != string::npos)
{
domain = url1.substr(pos1 + 3, pos2 - (pos1 + 3));
cout << "domain: " << domain << endl;
}
else
{
cout << "非法url" << endl;
}
string uri = url1.substr(pos2 + 1);
cout << "uri: " << uri << endl;
return 0;
}
getline(cin, s);
cin遇见空格会认为获取结束,当遇见一行字符串的时候,该字符串中间有空格,那么就不会获取到整行的字符串。getline会获取一行的字符串,遇到空格也不会认为获取终止。
以上就是今天要讲的内容,本文详细的介绍了STL简介、string的常用接口:常见构造、容量操作、访问及遍历操作、修改、非成员函数等。希望给友友们带来帮助!