@TOC
目录
STL:是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
今天,讲的就是STL容器里面的string。
1.构造函数
这7个构造函数的使用如下:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
//string ();
//构造一个空字符串,长度为0个字符
string s1;
//string (const char* s);
//复制s指向的以空结束的字符序列
string s2("hello world");
//string (const string& str);
//构造一个str的复制
string s3(s2);
//string (const string& str, size_t pos, size_t len = npos);
//复制str中从字符位置pos开始并跨越len个字符的部分。(如果str太短或len为string::npos,则复制到str的末尾)
string s4(s2, 2, 2);
//string (const char* s, size_t n);
//复制s指向的字符串的前n个字符
string s5("hello world", 5);
//string (size_t n,char c);
//用n个字符c来填充string
string s6(7, 's');
//string (InputIterator first, InputIterator last);
//以相同的顺序复制范围[first, last)中的字符序列
string::iterator begin = s5.begin();
string::iterator end = s5.end();
string s7(begin, end);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
cout << s7 << endl;
return 0;
}
2.析构函数
3.迭代器
迭代器一共有8种:
1.普通标记string开始位置的迭代器,
2.普通标记string结束位置的迭代器,
3.标记string反向开始位置的反向迭代器,
4.标记string反向结束位置的反向迭代器,
5.标记string开始位置的const迭代器,
6.标记string结束位置的const迭代器。
7.标记string反向开始位置的反向const迭代器,
8.标记string反向结束位置的反向const迭代器。
int main()
{
string s("hello world");
for (string::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it;
}
cout << endl;
for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++)
{
cout << *it;
}
cout << endl;
return 0;
}
4.内存函数
这些和内存有关系的函数中,size()和length()属于同一功能函数,我们在使用时常使用size(),因为在其他的容器中用的都是size()而没有length(),
另外max_size()表示的是string的一个字符串最长可以有多长,这是一个返回固定值的函数,作用也不是很大。
剩下的几个函数的作用很大,比如capaicty()
你会发现s的容量是15,31,47,70......当然这只是在Visual Studio这个编译器里面是这样变化的,在不同的系统中,变化是不同的。
resize()和reverse() 函数是两个扩容函数,如果你输入的数据比现有的字符串的容量小的时候, 他只会减小size,不会减小capacity,如果输入的数据大于现有字符串的容量时,会进行扩容直到容量大于输入的想要的容量。
empty()函数是用来判断字符串内容是否为空,clear()函数则是用来清空字符串的内容。
5.元素访问函数
(1) operator[]函数
char& operator[] (size_t pos);
//或者
const char& operator[] (size_t pos) const;
注:如果pos等于字符串长度,并且字符串是const限定的,则函数返回一个对空字符('\0')的引用。
operator[]函数是运算符重载函数,使用如下:
int main()
{
string s("hello world");
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
return 0;
}
(2)at函数
char& at (size_t pos);
//或者
const char& at (size_t pos) const;
这个函数可以返回字符串中pos位置的字符的引用。
同时自动检查pos是否是字符串中某个字符的有效位置(即pos是否小于字符串长度),如果不是则抛出out_of_range异常。
使用如下:
int main()
{
string s("hello world");
for (size_t i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
return 0;
}
(3)back函数和front函数
char& back();
//或者
const char& back() const;
char& front();
//或者
const char& front() const;
back函数可以返回一个字符串的最后一个字符的引用·
front函数可以返回一个字符串的第一个字符的引用
front函数与string::begin成员返回相同字符的迭代器不同,该函数返回直接引用。
注:back和front函数不能被空字符串调用
6.修改内容函数
(1)附加函数
operator+=函数
int main()
{
string s("hello world");
string str = "@@@";
//string& operator+=(const char* str);
s += "!!!";
//string& operator+=(const string& str);
s += str;
//string& operator+=(char c);
s += '@';
cout << s << endl;
return 0;
}
append函数
int main()
{
char* arr = "hello world ";
//string & append(const char* s);
//附加由s指向的以空结束的字符序列组成的字符串的拷贝。
string s1;
s1.append(arr);
//string& append(const string & str);
//追加str的拷贝
string s2;
s2.append(s1);
//string& append(const string & str, size_t subpos, size_t sublen);
//附加str的子字符串的拷贝。
//子字符串是str的一部分,从字符位置subpos开始并跨越子字符(或者直到str的末尾,如果str太短或如果sublen是string::npos)
string s3;
s3.append(s1, 1, 3);
//string& append(const char* s, size_t n);
//附加s指向的字符数组中前n个字符的拷贝
string s4;
s4.append(arr, 5);
//string& append(size_t n, char c);
//追加字符c的n个连续拷贝
string s5;
s5.append(5, 'c');
return 0;
}
push_back函数
void push_back(char c);
功能:将字符c追加到字符串的末尾,将其长度增加1
int main()
{
string s;
s.push_back('h');
s.push_back('e');
s.push_back('l');
s.push_back('l');
s.push_back('o');
cout << s << endl;
return 0;
}
(2)insert函数
与附加函数不同,insert的功能更加强大。
//插入str的拷贝
string& insert (size_t pos, const string& str);
//插入str的子字符串的拷贝。子字符串是str的一部分,从字符位置subpos开始并跨越子字符(或者直到str的末尾,如果str太短或如果sublen为npos)。
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
//插入由s指向的以空结束的字符序列组成的字符串的拷贝。
string& insert (size_t pos, const char* s);
//在s指向的字符数组中插入前n个字符的拷贝。
string& insert (size_t pos, const char* s, size_t n);
//插入字符c的n个字符拷贝
string& insert (size_t pos, size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
//插入字符c
iterator insert (const_iterator p, char c);
//以相同的顺序插入范围[first, last)中的字符序列的副本。
template <class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);
(3)erase函数
int main()
{
string s = "hello world";
s.erase(6, 5);
cout << s << " " << s.size() << endl;
string::iterator it1 = s.begin();
s.erase(it1);
cout << s << " " << s.size() << endl;
string::iterator it2 = s.begin();
string::iterator it3 = s.end();
s.erase(it2, it3);
cout << s << " " << s.size() << endl;
return 0;
}
(4)replace函数
//拷贝str
string& replace (size_t pos, size_t len, const string& str);
string& replace (const_iterator i1, const_iterator i2, const string& str);
//复制str中从字符位置subpos开始并跨越子字符的部分(如果str太短或subblen为string::npos,则复制到str的末尾)。
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
//复制s指向的以空结束的字符序列
string& replace (size_t pos, size_t len, const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);
//从s指向的字符数组中复制前n个字符
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
//用字符c的n个连续拷贝替换字符串的一部分
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
//以相同的顺序复制范围[first, last)中的字符序列
template <class InputIterator> string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
(5)pop_back函数
void pop_back();
功能:擦除字符串的最后一个字符,有效地将其长度减少1。
操作和push_back类似
7.字符串操作函数
(1)c_str函数
返回一个指向数组的指针,该数组包含以空结束的字符序列(即C-string),表示字符串对象的当前值。
该数组包含组成string对象值的相同字符序列,并在末尾加上一个额外的终止空字符('\0')。
(2)find函数
在字符串中搜索由其参数指定的序列的第一次出现。
当指定pos时,搜索只包括位于pos位置或之后的字符,忽略任何可能出现的包括pos位置之前的字符。
注意,与成员find_first_of不同,只要搜索多个字符,只匹配其中一个字符是不够的,整个序列必须匹配。
上面的STL函数属于是string的常用函数,想了解其他的函数可以在cplusplus网中去学习,下个博客会讲一下string的就简单实现。