一:基本功能介绍(初始化,析构等等)
1:构造(constructor)
(1)string()(默认构造,不带参)
(2)string(const string& str)(拷贝构造)
(3)string(const char* s)(用一个字符串对其进行初始化,类似于赋值)
(5)string(const string& str, size_t pos, size_t len = npos)
(将另一个字符串从某一个位置开始,用len个字符进行初始化)
(6)直接使用已经重载过的"="(这种方法最为常用)
void text1()
{
/*string s1();*/
string s2("11111");
string s3(s2);
string s4(s2, 0, 2);
string s5 = "11111";
//cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
}
PS:1:npos为静态成员变量,为size_t类型,值为-1
2:流插入与流提取已经均被重载过,可以直接使用
二:遍历的实现
字符串长度的返回:size(),len()(两者返回的均为字符串的长度,为了保持后面容器的泛性,常 使用size())
对string中的字符进行遍历一共有三种方法:operator[],迭代器,范围for
1:operator[]
将[]进行重载,使得可以像访问数组一样去访问string
string提供了两种形式:
char& operator[](size_t pos) const char& operator[](size_t pos) const
前一种可以直接修改对应位置上的值,后一种返回值被const修饰,故只可以对其进行访问,在实际运用中可以根据不同的需求进行编译和选择
string s1 = "23456";
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
at:at功能与operator[]一致,但是遇到越界访问问题,operator[]会直接暴力报错,因为底层是用assert进行实现的,而at会比较温和,不会直接报错
PS:传引用返回的两种优势:1:减少拷贝的次数
2:使得返回值可以被修改
2:迭代器
正向:
begin()返回第一个字符位置的指针
end()返回最后一个字符位置的指针
cbegin()依旧返回第一个字符位置的指针,但是其指向的内容不可以进行修改
cend()依旧返回最后一个字符位置的指针,但是其指向的内容不可以进行修改
逆向:
rbegin()返回最后一个字符位置的指针
rend()返回第一个字符位置的指针
crbegin()依旧返回最后一个字符位置的指针,但是其指向的内容不可以进行修改
crend()依旧返回第一个字符位置的指针,但是其指向的内容不可以进行修改
string s1 = "hello world";
string::iterator it1 = s1.begin();
while (it1 != s1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
string::const_iterator it2 = s1.begin();
while (it2 != s1.end())
{
cout << *it2 << " ";
++it2;
}
cout << endl;
string::reverse_iterator it3 = s1.rbegin();
while (it3 != s1.rend())
{
cout << *it3 << " ";
++it3;
}
cout << endl;
string::const_reverse_iterator it4 = s1.rbegin();
while (it4 != s1.rend())
{
cout << *it4 << " ";
++it4;
}
cout << endl;
3:范围for
实现自动获取数据,自动进行加加,传参为传引用时,可以对该位置的值进行修改
string s2 = "hello world";
for (auto e : s2)
{
cout << e << " ";
}
PS:范围for的底层就是迭代器,不要随便使用!!!
Sum:string中常使用的为下标,容器之间通用的为迭代器与范围for!!!
三:修改
尾插:
1:push_back()
2:append()
string& append(const string& str)(直接尾插一个字符串)
3:+=(使用运算符重载,更为常用)
string s3 = "hello,world";
s3 += 'a';
cout << s3<<endl;
s3.append("12");
cout << s3<<endl;
s3.push_back('a');
cout << s3<<endl;
尾删:pop_back()
头插:insert
string& insert(size_t pos, const string& str)(在固定的位置前头插一个字符串)
string& insert(size_t pos, size_t n, char c)(在固定位置前头插n个字符)
void insert(operator p, size_t n, char c);(使用迭代器进行头插)
string& insert(size_t pos, const string& str,size_t subpos, size_t sublen)
(将另一个字符串的一部分插入到字符串中)
string s4 = "hello world";
s4.insert(0, 1, '1');//下标头插一个字符
cout << s4 << endl;
s4.insert(0, "12");
cout << s4 << endl;
s4.insert(s4.begin(), 'a');//迭代器进行头插一个字符
cout << s4 << endl;
头删:erase
string& erase(size_t pos = 0, size_t len = npos)(从固定位置头删len个字符)
PS:下标位置不可以太离谱!!!
string s5 = "hello world";
s5.erase(0, 3);
cout << s5 << endl;
替换:replace
string& replace(size_t pos, size_t len, const string& str)
(将固定位置的len个字符替换为字符串str)
s5.replace(0, 3, "111");
cout << s5 << endl;
四:容量
capacity :容量
resize:控制size,在一定情况下,也会影响到capacity
void resize(size_t n)
void resize(size_t n, char c)
reserve:控制capacity(适用于知道需要多少空间,直接开空间的情况)
void reserve(size_t n = 0)
string s6 = "hello world";
cout << s6.capacity()<<endl;
s6.resize(5, 'a');
cout << s6.capacity()<<endl;
s6.reserve(20);
cout << s6.capacity()<<endl;
PS:在vs下,并不一定会按照给出的值扩容,capacity可能会比给出的要大,而在linus下,会与给出的一样
五:其他操作
1:c_str
2:find(查找)
size_t find(char c, size_t pos = 0)(从某个位置查找特定字符,并返回其位置)
size_t find(const string& str, size_t pos = 0)(从某个特定位置查找特定字符串,并返回其位置)
3:rfind(逆向查找)
size_t rfind(const string& str, size_t pos = npos)
size_t rfind(char c, size_t pos = npos)
4:substr
string substr(size_t pos = 0, size_t len = npos) const
(返回从pos位置开始后面的len个字符)
string s7 = "hello world";
size_t i = s7.find('l', 0);
cout << i << endl;
size_t i1 = s7.rfind('o');
cout << i1 << endl;
string s8;
s8 = s7.substr(5);
cout << s8 << endl;
5:getline
istream& getline(itream& is, string& str, char delim)(以固定的字符delim作为结束)
istream& getline(itream& is, string& str)(以换行作为结尾,停止输入)
6:to_string
将整型转化为字符串,最为常用