string
C语言中实际没有专门的字符串类型,如果要表示字符串:char*或字符数组(注意\0)
C语言还提供了一个专门用来进行字符串操作的库函数<string.h>;
strlen()/strcpy()/strcmp()/strstr()/strcat()/strchr().... 数据和方法是分离开的,不满足面向对象的思想
在OJ中,字符串一般都是通过string给出的
string的常用接口
1.string类对象的常见构造
- string():构造空的string类对象,即空字符串,注意:\0;
- string(size_t n,char ch):string类对象中包含n个字符c;
- string(char* str):用C格式的字符串构造string对象;
- string(const string& s):拷贝构造;
void Teststring()
{
string s1;//构造空的string类对象
string s2("hello");//用C格式字符串构造string类对象s2
string s3(s2);//拷贝构造s3
}
2.string类对象的容量操作
- size/length:返回字符串有效字符长度;
- capacity:返回空间总大小;
- empty:检测字符串释放为空串,是返回true,否则返回false;
- clear:清空有效字符;
- reserve:为字符串预留空间;
- resize:将有效字符的个数该成n个,多出的空间用字符c填充;
// size/clear/resize
void Teststring1()
{
// 注意:string类对象支持直接用cin和cout进行输入和输出
string s("hello, bit!!!");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
s.resize(10, 'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
// 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
// 注意此时s中有效字符个数已经增加到15个
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
// 将s中有效字符个数缩小到5个
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
void Teststring2()
{
string s;
// 测试reserve是否会改变string中有效元素个数
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
// 测试reserve参数小于string的底层空间大小时,是否会将空间缩小
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
// 利用reserve提高插入数据的效率,避免增容带来的开销
void TestPushBack()
{
string s;
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
void TestPushBackReserve()
{
string s;
s.reserve(100);
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
注意:
- size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
- clear()只是将string中有效字符清空,不改变底层空间大小。
- resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
- reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
3.string类对象的访问及遍历操作
- operator[] :返回pos位置的字符,const string类对象调用;
- begin+end:begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器;
- rbegin+rend:与正向迭代器相反
- 范围for:C++11支持更简洁的范围for的新遍历方式
void TestString()
{
string s1("hello");
const string s2("Hello");
cout << s1 << " " << s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
s1[0] = 'H';
cout << s1 << endl;
}
void TestString()
{
string s("hello");
// 1. for+operator[]
for (size_t i = 0; i < s.size(); ++i)
cout << s[i] << endl;
// 2.迭代器
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it << endl;
++it;
}
string::reverse_iterator rit = s.rbegin();
while (rit != s.rend())
cout << *rit << endl;
// 3.范围for
for (auto ch : s)
cout << ch << endl;
}
4.string类对象修改操作
- operator+= :在字符串后追加字符串;
- push_back:在字符串后尾插字符
- append:在字符串后追加一个字符串;
- c_str:返回C格式字符串;
- find+npos:从字符串pos位置开始往后找字符,返回该字符在字符串中的位置;
- rfind:从字符串pos位置开始往前找字符,返回该字符在字符串中的位置;
- substr:在str中从pos位置开始,截取n个字符,然后将其返回;
void Teststring()
{
string str1;
str1.push_back(' '); // 在str后插入空格
str1.append("hello"); // 在str后追加一个字符"hello"
str1 += 'e'; // 在str后追加一个字符'b'
str1 += "veryone"; // 在str后追加一个字符串"it"
cout << str1 << endl;
cout << str1.c_str() << endl; // 以C语言的方式打印字符串
// 获取everyone
string str2("hello everyone");
size_t pos = str2.rfind(' ');
string suffix(str2.substr(pos, str2.size() - pos));
cout << suffix << endl;
// 取出url中的域名
string str3("https://blog.csdn.net/independenting?spm=1011.2124.3001.5343");
cout << str3 << endl;
size_t start = str3.find("://");
if (start == string::npos)
{
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = str3.find('/', start);
string address = str3.substr(start, finish - start);
cout << address << endl;
// 删除url的协议前缀
pos = str3.find("://");
str3.erase(0, pos + 3);
cout << str3 << endl;
}