目录
一、string类对象的常见构造
string(); //构造空的string类对象,即空字符串
string (const string& str);//拷贝构造函数
string (const string& str, size_t pos, size_t len = npos);
string (const char* s);//用C-string来构造string类对象
string (const char* s, size_t n);
string (size_t n, char c);
二、string类对象的容量操作
函数名称 | 功能说明 |
size(重点) | 返回字符串有效字符长度 |
length | 返回字符串有效字符长度 |
capacity | 返回空间总大小 |
empty (重点) | 检测字符串释放为空串,是返回true,否则返回false |
clear (重点) | 清空有效字符 |
reserve (重点) | 为字符串预留空间** |
resize (重点) | 将有效字符的个数该成n个,多出的空间用字符c填充 |
size
size_t size() const;
注:size是有效字符长度,'\0'不算,例如"hello string"的size()=12
reserve
重新分配存储的大小
void reserve (size_t n = 0);
注:a、此容量不一定等于字符串长度。它可以等于或大于此值,额外的空间允许对象在向字符串中添加新字符时优化其操作
b、n小于原有容量,则不做任何处理
resize
调整字符串大小,将字符串大小调整为n个字符
void resize (size_t n);
void resize (size_t n, char c);
注: a.当n大于当前的size时,将size扩大到n,扩大的字符为ch,若ch未给出,则默认为’\0’。
b.当n小于当前的size时,将size缩小到n。
clear
将字符串将变为空字符串(长度为0个字符)
void clear();
empty
返回字符串是否为空(即其长度是否为0)
bool empty() const;
测试用例:
void test_string7()
{
string st1("0123456789");
cout << st1.max_size() << endl;//字符串长度的理论限制由成员max_size给出。
//2147483647
string st3;
st3.reserve(100);//保留 开空间
st3.reserve(50); //小于原有容量,则不做任何处理
st3.resize(200,'Z'); // 开空间 + 初始化
cout << endl;
size_t sz = st1.capacity();
sz = st1.capacity();
cout << "capacity changed: " << sz << '\n';
cout << "making s grow:\n";
for (int i = 0; i < 1000; ++i)
{
st1.push_back('c');
if (sz != st1.capacity())
{
sz = st1.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
三、string类对象的访问及遍历操作
函数名称 | 功能说明 |
operator[ ] (重点) | 返回pos位置的字符,const string类对象调用 |
begin+ end | begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器 |
rbegin + rend | begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器 |
范围for | C++11支持更简洁的范围for的新遍历方式 |
begin
返回指向字符串第一个字符的迭代器
iterator begin();
const_iterator begin() const;
end
返回一个迭代器,该迭代器指向字符最后一个字符的下一个字符的位置
iterator end();
const_iterator end() const;
注:迭代器功能上类似指针,但不是指针
rbegin
返回指向字符串最后一个字符的反向迭代器(即其反向开头)
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
注:反向迭代器向后迭代:增加它们会将它们移向字符串的开头
rend
返回一个反向迭代器,该迭代器指向字符串第一个字符之前的理论元素(被视为其反向结束)
reverse_iterator rend();
const_reverse_iterator rend() const;
测试用例:
void test_string5()
{
string s1("hello string !!!");
string::iterator it = s1.begin();//指明string里面的迭代器
while (it != s1.end())
{
(*it)++;
cout << *it << " ";
++it;
}
cout << endl;
cout <<"s1 :"<< s1 << endl;
string s2("hello world");
string::reverse_iterator rit = s2.rbegin();
while (rit != s2.rend())
{
*rit = 'x';
cout << *rit << " ";
rit++;
}
cout << endl;
cout << "s2 :"<<s2 << endl;
const string str("hello world");
string::const_reverse_iterator itr = str.rbegin();//对于const修饰的string类型,只能读,不能修改
while (itr != str.rend())
{
cout << *itr << " ";
++itr;
}
cout << endl;
cout << "str :" << str << endl;
}
operator[ ]
返回对字符串中位置pos处字符的引用,可像数组一样遍历查找
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
void test_string5()
{
string s2("string");
for (size_t i = 0; i < s2.size(); i++)
{
cout << s2[i] << " ";
}
cout << endl;
}
范围for
范围for底层其实就是迭代器,自动迭代,自动判断结束
void test_string4()
{
string s2("string");
// 范围for -- 自动迭代,自动判断结束
// 依次取s中每个字符,赋值给ch
for (auto ch : s2)//ch只是s2的一份拷贝
{
ch++;
cout << ch << " ";
}
cout << endl;
cout << s2 << endl;
for (auto& a : s2)//加上&,a的修改会影响s2
{
a++;
cout << a << " ";
}
cout << endl;
cout << s2 << endl;
}
四、string类对象的修改操作
函数名称 | 功能说明 |
push_back | 在字符串后尾插字符c |
append | 在字符串后追加一个字符串 |
operator+= (重点) | 在字符串后追加字符串str |
c_str(重点) | 返回C格式字符串 |
find + npos(重点) | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
rfind | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
substr | 在str中从pos位置开始,截取n个字符,然后将其返回 |
push_back
在字符串后尾插字符c
void push_back (char c);
operator+=
string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
append
在字符串后追加一个字符串
string& append (const string& str);
string& append (const string& str, size_t subpos, size_t sublen);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
测试用例:
void test_string5()
{
string str("hello string");
str.push_back('*');
str.push_back('*');
str.push_back('*');
cout << str << endl;
string st("123456");
st += "*****";
st +=">>>>>";
st+=str;
cout << st << endl;
string stl("123456");
str.append(stl,1,7);//string& append (const string& str, size_t subpos, size_t sublen);
cout << str << endl;
}
c_str
获取等效C字符串,返回一个指向数组的指针(此数组包含组成字符串对象值的相同字符序列,并在末尾添加一个额外的终止空字符'\0')
const char* c_str() const;
void test_string8()
{
string filename("test.cpp");
cout << filename << endl;
cout << filename.c_str() << endl;
FILE* fout = fopen(filename.c_str(), "r");
assert(fout);
int ch = 0;
while ((ch = fgetc(fout)) != EOF)
{
printf("%c", ch);
}
}
copy
将string的子字符串复制到字符数组中
size_t copy (char* s, size_t len, size_t pos = 0) const;
void test_string4()
{
string filename("test.cpp");
cout << filename << endl;
cout << filename.c_str() << endl;
filename += '\0';
filename += "string.cpp";
cout << filename << endl; // string 对象size为准
cout << filename.c_str() << endl; // 常量字符串对象\0
cout << filename.size() << endl;
string copy = filename;
cout << copy << endl;
}
find
在字符串中搜索由其参数指定的序列的第一个匹配项。
指定pos时,搜索仅包括pos处或pos后的字符,忽略包含pos前字符的任何可能出现的情况。
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
rfind
在字符串中搜索由其参数指定的序列的最后一个匹配项。
当指定pos时,搜索只包括在pos位置或之前开始的字符序列,忽略pos之后开始的任何可能的匹配
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;
测试用例:
void DealUrl(const string& url)
{
size_t pos1 = url.find("://");
if (pos1 == string::npos)
{
cout << "非法url" << endl;
return;
}
string protocol = url.substr(0, pos1);
cout << protocol << endl;
size_t pos2 = url.find('/', pos1 + 3);
if (pos2 == string::npos)
{
cout << "非法url" << endl;
return;
}
string domain = url.substr(pos1 + 3, pos2 - pos1 - 3);
cout << domain << endl;
string uri = url.substr(pos2 + 1);
cout << uri << endl << endl;
}
void test_string10()
{
string filename("test.cpp.tar.zip");
// 后缀
//size_t pos = filename.find('.');
size_t pos = filename.rfind('.');
if (pos != string::npos)
{
//string suff = filename.substr(pos, filename.size() - pos);
string suff = filename.substr(pos);
cout << suff << endl;
}
string url1 = "https://cplusplus.com/reference/string/string/";
string url2 = "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=ascall&step_word=&hs=0&pn=0&spn=0&di=7108135681917976577&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=2613959014%2C543572025&os=2740573600%2C1059518451&simid=2613959014%2C543572025&adpicid=0&lpn=0&ln=179&fr=&fmq=1660115697093_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fimg.php.cn%2Fupload%2Fimage%2F147%2F157%2F796%2F1593765739620093.png%26refer%3Dhttp%3A%2F%2Fimg.php.cn%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1662707704%26t%3Da68cb238bbb3f99d0554098c785d526e&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Brir_z%26e3BvgAzdH3FuwqAzdH3F9c9amd_z%26e3Bip4s&gsm=1&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDIsNCw2LDEsNSw3LDgsOQ%3D%3D";
string url3 = "ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf";
DealUrl(url1);
DealUrl(url2);
DealUrl(url3);
}
substr
从字符位置pos开始,跨越len字符(或直到字符串末尾,以先到者为准)生成子字符串
返回一个新构造的字符串对象
string substr (size_t pos = 0, size_t len = npos) const;
void test_string5()
{
string str("hello string");
string s1 = str.substr(0, 4);
cout << s1 << endl; //hell
}
五、string类非成员函数
函数名称 | 功能说明 |
operator+ | 尽量少用,因为传值返回,导致深拷贝效率低 |
operator<< (重点) | 输入运算符重载 |
operator>> (重点) | 输出运算符重载 |
getline (重点) | 获取一行字符串 |
relational operators (重点) | 大小比较 |
getline
从流中获取行到字符串
从is中提取字符并将其存储到str中,直到找到分隔符delim(或换行符'\n')
istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);
void test_string11()
{
string s2;
getline(cin, s2);//hello string
cout << s2 << endl;
}
注:getline()可以读取空格字符(cin不能)
relational operators
字符串的关系运算符
bool operator== (const string& lhs, const string& rhs);
bool operator!= (const string& lhs, const string& rhs);
bool operator< (const string& lhs, const string& rhs);
bool operator<= (const string& lhs, const string& rhs);
bool operator> (const string& lhs, const string& rhs);
bool operator>= (const string& lhs, const string& rhs);
void test_string11()
{
string s1;
string s2;
cin >> s1 >> s2;
cout << (s1 > s2) << endl;
}