文章目录
- 1.初识string类
- 2.string类对象的常见构造
- 3.string类对象的容量操作
- 4.string类对象的访问
- 5.string中迭代器及元素的遍历
- 6.string类对象的修改操作
- 7.string类非成员函数
1.初识string类
- string是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
- string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>
string; - 不能操作多字节或者变长字符的序列。
2.string类对象的常见构造
1)string()
2)string (const string& str)
3)string (const string& str, size_t pos, size_t len = npos)
4)string (const char* s)
5)string (const char* s, size_t n)
6)string (size_t n, char c)
7)template
3.string类对象的容量操作
1)size
size_t size() const;
2)length
size_t length() const;
3)max_size
size_t max_size() const;
这是在VS2019下运行的结果,不同编译器可能不同
4)resize
void resize (size_t n);
void resize (size_t n, char c);
- newsize<=oldsize
- newsize>oldsize&&newsize<=capacity
- newsize>oldsize&&newsize>capacity
区别在于前一个的后续元素以\0填充,后一个则由字符a填充
5)capacity
size_t capacity() const;
6)reserve
void reserve (size_t n = 0);
在vs2019下,string是按照1.5倍的方式扩容,在linux下是按照2倍的方式扩容
7)clear
void clear();
4.string类对象的访问
1)operator[]
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
如果越界访问,会触发assert断言,终止程序
2)at
char& at (size_t pos);
const char& at (size_t pos) const;
3)back(c++11)
char& back();
const char& back() const;
4)front(c++11)
char& front();
const char& front() const;
5.string中迭代器及元素的遍历
1)begin&&end
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
2)rbegin&&rend
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
3)范围for遍历
int main()
{
string s1("hello world!");
for (auto e : s1)
{
cout << e;
}
return 0;
}
4)迭代器遍历
int main()
{
string s1("hello world!");
string::iterator p = s1.begin();
while (p != s1.end())
{
cout << *p;
p++;
}
return 0;
}
5)for循环+[]遍历
int main()
{
string s1("hello world!");
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i];
}
return 0;
}
6.string类对象的修改操作
1)operator+=
string& operator+= (const string& str);//末尾追加string对象
string& operator+= (const char* s);//末尾追加s字符串
string& operator+= (char c);//末尾追加c字符
2)append
string& append (const string& str);//末尾追加str
string& append (const string& str, size_t subpos, size_t sublen);末尾追加从str的subpos开始,长度为sublen的子串
string& append (const char* s);//末尾追加一个s字符串
string& append (const char* s, size_t n);//末尾追加一个s字符串从0号下标开始长度为n的子串
string& append (size_t n, char c);//末尾追加n个字符c
template <class InputIterator>
string& append (InputIterator first, InputIterator last);//追加迭代器first到last区间的内容
3)assign
string& assign (const string& str);
string& assign (const string& str, size_t subpos, size_t sublen);
string& assign (const char* s);
string& assign (const char* s, size_t n);
string& assign (size_t n, char c);
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
4)push_back
void push_back (char c);
5)insert
string& insert (size_t pos, const string& str);//在pos处插入string对象str
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);//在pos处插入string对象str从subpos开始长为sublen的子串
string& insert (size_t pos, const char* s);//在pos处插入字符串s
string& insert (size_t pos, const char* s, size_t n);//在pos处插入字符串s的前n个字符
string& insert (size_t pos, size_t n, char c);
void insert (iterator p, size_t n, char c);//在pos处插入n个字符c
iterator insert (iterator p, char c);//在p处插入字符c
template <class InputIterator>
void insert (iterator p, InputIterator first, InputIterator last);//利用迭代器在p处插入first到last的子串
6)erase
string& erase (size_t pos = 0, size_t len = npos);//从pos出开始,删除长度为len的子串,pos和npos均为缺省参数,默认删掉整个字符串
iterator erase (iterator p);//删除p位置元素,返回下一个元素位置
iterator erase (iterator first, iterator last);删除区间fairst到last间元素
7)swap
void swap (string& str);
7.string类非成员函数
1)find
size_t find (const string& str, size_t pos = 0) const;//在目标字符串中查找str字符串出现的位置,默认从头开始查找。找到返回str首个字符在目标字符串中的位置,找不到返回npos
size_t find (const char* s, size_t pos = 0) const;//功能与上一个一致,查询字符串s出现的位置
size_t find (const char* s, size_t pos, size_t n) const;//查询字符串s的前n个字符
size_t find (char c, size_t pos = 0) const;//查询字符c
2)rfind
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const;
3)substr
string substr (size_t pos = 0, size_t len = npos) const;