String类
- string在底层实际是:basic_string模板类的别名,
typedef basic_string<char> string;
- 在使用string类时,必须包含#include头文件以及using namespace std;
string常见接口
1.1 string类对象的常见构造
1.string(): 构造空的string类对象,即空字符串
2.string(const char s)*: 用C-string(c格式的字符串)来构造string类对象
3.string(size_t n,char c): string类对象中包含n个字符c
4.string(const string& s): 拷贝构造函数
示例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s1;//空字符串
string s2("hello");
string s3(5, 'A');
string s4(s2);//拷贝构造
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
1.2 string类对象的容量操作
size_t size() const;
返回字符串中现在拥有的字符数,不计算’\0’
size_t length() const;
返回字符串的长度和size()返回的数字相同
- size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
size_t capacity() const:
返回的总空间大小不包括‘\0’所占空间
bool empty():
如果字符串为空则empty()返回真(true),否则返回假(false).
void clear(): 清空有效字符
只是将string中有效字符清空,不改变底层空间大小
void reserve(size_t n=0)
: 为字符串预留空间,知道最多需要多少空间的情况下,可以省去增容的开销,为string预留空间,不改变有效元素个数。
当n大于capacity时,将容量增加到n,小于capacity时不变。
resize:
void resize (size_t n);
void resize (size_t n, char c);
将有效字符的个数改成n个,多出的空间用字符c填充,如果没有传字符c就默认填‘\0’。
注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
使用示例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s1("hello world");
string s2("");
//size()
cout << s1.size() << endl;//11
cout << s2.size() << endl;//0
//length()
cout << s1.length() << endl;//11
cout << s2.length() << endl;//0
//capacity()
cout << s1.capacity() << endl;//15
cout << s2.capacity() << endl;//15
//reserve()
s2.reserve(100);
cout << s2.capacity() << endl;//111
//resize()
s1.resize(15, 'x');
for (auto ch : s1)
{
cout << ch;//hello worldxxxx
}
cout << endl;
s1.resize(5);
for (auto ch : s1)
{
cout << ch;//hello
}
//clear()清空
s1.clear();
cout << s1 << endl;
return 0;
}
1.3 访问遍历
operator[ ]
- 返回pos位置的字符,const string类对象调用
begin+ end:
iterator begin();
- begin获取第一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rend :
reverse_iterator rbegin();
- rbegin获取指向字符串的最后一个字符的反向迭代器 + rend获取指向字符串的第一个字符之前的理论元素。
范围for:
- C++11支持更简洁的范围for的新遍历方式
使用示例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s("hello");
//1、size+[]
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//2、迭代器
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;
//范围for(C++11)
for (auto ch : s)
{
cout << ch << " ";
}
}
1.4 修改操作
-
push_back:
void push_back (char c);
- 在字符串后尾插字符c
-
append:
- 在字符串后追加一个字符串
-
operator+=
- 在字符串后追加字符或字符串
-
c_str
- 返回C格式字符串
- find + npos
size_t find(const char ch,size_t pos = 0);//从pos个位置开始查找字符ch第一次出现的位置,如果没有返回npos
size_t find(const char* str,size_t pos = 0);//从pos个位置开始找字符串str,如果没有返回npos
- rfind
- 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
- substr: string substr (size_t pos = 0, size_t len = npos) const;
- 在str中从pos位置开始,截取n个字符,然后将其返回
- insert
void insert(size_t,char* str);//在pos位置插入一个字符串
void insert(size_t pos,char ch);//在pos位置插入一个字符ch
- erase 清除字符或字符串
使用实例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s("hello world");
//push_back 尾插字符
s.push_back('!');
cout << s << endl;//hello world!
//append 尾插字符串
s.append("C++");
cout << s << endl;//hello world!C++
//operator+=
string s1("hello");
string s2("world");
s1 += ' ';
s1 += s2;
cout << s1 << endl;//hello world
//c_str
cout << s1.c_str() << endl;//hello world
//find
size_t pos = s1.find(' ');
cout << pos << endl;//5
//rfind
size_t rpos = s1.rfind(' ');
cout << rpos << endl;//5
//substr 从pos位置开始阶段
cout << s1.substr(pos) << endl;// world
//insert
cout << s1.insert(pos, " C++") << endl;//hello C++ world
//erase 清除
s1.erase(pos, 4);
cout << s1 << endl;//hello world
return 0;
}