网站分析表,深圳广电制作中心,网站建设 APP开发销售怎么做,网站建设维护百家号CSDN的uu们#xff0c;大家好。这里是C入门的第十六讲。 座右铭#xff1a;前路坎坷#xff0c;披荆斩棘#xff0c;扶摇直上。 博客主页#xff1a; 姬如祎 收录专栏#xff1a;C专题 目录
1.构造函数
1.1 string()
1.2 string(const char* s)
1.3 string(const … · CSDN的uu们大家好。这里是C入门的第十六讲。 · 座右铭前路坎坷披荆斩棘扶摇直上。 · 博客主页 姬如祎 · 收录专栏C专题 目录
1.构造函数
1.1 string()
1.2 string(const char* s)
1.3 string(const char* s, size_t n)
1.4 string(size_t n, char c)
1.5 string (const string str)
1.6 string (const string str, size_t pos, size_t len npos)
2. size_t size() const
3. size_t length() const
4. size_t max_size() const
5. resize
5.1 void resize (size_t n)
编辑5.2 void resize (size_t n, char c)
6. size_t capacity() const
7: void reserve (size_t n 0)
8. void clear()
9. bool empty() const
10. operator[]
11. char at(size_t pos)
12. char back()
13. char front()
14. operator
15. string append (const char* s)
编辑
16. void push_back (char c)
17. insert()
17.1 string insert (size_t pos, const char* s)
17.2 string insert (size_t pos, size_t n, char c)
18. string erase (size_t pos 0, size_t len npos)
19. void pop_back()
20. const char* c_str() const
21. find()
21.1 size_t find (const char* s, size_t pos 0) const
21. 2 size_t find (char c, size_t pos 0) const
22. rfind()
编辑
22.1 size_t rfind (const char* s, size_t pos npos) const
22.2 size_t rfind (char c, size_t pos npos) const
23. string substr (size_t pos 0, size_t len npos) const 1.构造函数
1.1 string()
这是 string 的无参构造在 string 的底层就是构造了一个空字符串“”即在下标为 0 的位置存储了一个 \0 。
#includeiostream
#includestring
using namespace std;int main()
{string s;cout s;return 0;
} 这是VS2022下看到的结果。VS对 string 的封装比较复杂他一上来就开了很多空间并且直接把开辟出来的空间全部初始化为了0。我们看到的现象会因编译器的不同有所差异。
1.2 string(const char* s)
这个构造函数表示可以使用常量字符串来初始化 string或者使用字符数组来初始化字符数组的数组名也是 char* 类型的嘛char* 是可以用const char* 来接收的。
#includeiostream
#includestring
using namespace std;int main()
{string s(csdn yyds!);cout s endl; //输出csdn yyds!char arr[12] { c, s, d, n, ,y ,y, d, s, !};string s1(arr);cout s1 endl; //输出csdn yyds!}
1.3 string(const char* s, size_t n) 这个构造函数表示可以使用一个常量字符串 或者 字符数组的前 n 个字符来初始化 string。
#includeiostream
#includestring
using namespace std;int main()
{string s(csdn yyds!, 4);cout s endl; //输出csdnchar arr[10] { c, s, d, n, ,y ,y, d, s };string s1(arr, 4);cout s1 endl; //输出csdn
}
1.4 string(size_t n, char c)
这个构造函数表示可以用 n 个 字符 来构造一个string对象。
#includeiostream
#includestring
using namespace std;int main()
{string s(8, 8);cout s endl; //输出88888888
}
1.5 string (const string str)
这个构造函数表示可以使用另一个string对象来构造一个string对象(拷贝构造)。
#includeiostream
#includestring
using namespace std;int main()
{string s(csdn yyds!);string s1(s);cout s1 endl; //输出csdn yyds!
}
1.6 string (const string str, size_t pos, size_t len npos)
这个构造函数表示可以用另一个 string 对象 从 pos 位置开始截取 n 个字符来构造一个 string 对象。
我们看到 len 参数有个缺省值npos。这个 npos 是定义在 string 类中一个public属性的静态常量其值为 -1。但是因为 len 的类型为无符号的整形因此 len 的实际大小是无符号整形的最大值。我们可以直接打印 npos
#includeiostream
#includestring
#includeclimits
using namespace std;int main()
{cout (int)string::npos endl; //int输出的nposcout string::npos endl; //size_t输出的nposcout UINT32_MAX endl; //32位机器下无符号整形的最大值} 当我们的 pos len 大于参数一字符串的长度时就是截取 pos 位置后的全部字符来构造string。不传 len len npos 那可是相当大呢所以不传 len 就是截取 pos 后面的全部字符来构造string。
#includeiostream
#includestring
using namespace std;int main()
{string s(csdn yyds!);string s1(s, 5, 4); //从下标为 5 的位置(第一个y)向后截取 4 个字符来构造一个stringcout s1 endl; //输出 yydsstring s2(s, 5, 188); //len 传入很大cout s2 endl; //输出 yyds!string s3(s, 5); //不传lencout s3 endl; //输出 yyds!}
2. size_t size() const 这个函数用于返回一个 string 对象存储的有效字符的数量。为什么要有后面的那个const呢我们知道这个const是修饰 *this 的加上const 常对象与普通对象都可以调用啦
#includeiostream
#includestring
using namespace std;int main()
{string s1(I love scdn!);cout s1.size() endl; //输出12const string s2(csdn yyds!);cout s2.size() endl; //输出10}
3. size_t length() const 这个接口跟 size_t size() const 实现的效果是一毛一样的那你就可能会问了为什么设计者要搞出来两个功能相同的接口呢那是因为 string 类的出现早于 STL 库string 在很久以前是没有 size() 这个接口的。但是呢 STL出现以后求容器的大小都是实现的 size(), 于是 string 就也加上了一个 size() 接口
4. size_t max_size() const
这个函数返回 一个string 对象多能存储的最多的字符数量每个编译器的结果不尽相同此函数用得也不多 5. resize
5.1 void resize (size_t n)
在 string 的底层维护了一个字符数组一个表示有效字符个数的size和字符数组实际的容量capacity。而 resize 函数就是用来改变有效字符的个数的函数。
情况1当 n 小于原字符串的大小底层实现很简单直接改变size在添加一个 \0 表示字符的结束即可 因此会保留原字符串的数据。
#includeiostream
#includestring
using namespace std;int main()
{string s1(csdn yyds);s1.resize(4);cout s1 endl; //输出csdncout s1.size() endl; //输出4}
通过调试观察 string 的内存也可以证明我的描述 情况2 当 n 大于原字符串的大小。会将原字符串的 size 扩展到相应的大小但是在 VS2022 上多余的空间他会全部初始化为 0 resize 之后的字符串的打印结果与原来并无不同。但是可以通过打印 size 来观察不同
#includeiostream
#includestring
using namespace std;int main()
{string s1(csdn yyds!);cout s1.size() endl; //resize之前输出10s1.resize(20);cout s1 endl; //输出csdn yyds!cout s1.size() endl; //输出20
}
通过观察 string 内存能够证实我的讲解
5.2 void resize (size_t n, char c) 这是一个重载的版本在改变字符串大小的同时增加了初始化的功能这个初始化的功能仅在 n 的大小大于原字符串的大小时生效(也就是 5.1 情况二的时候生效。)
#includeiostream
#includestring
using namespace std;int main()
{string s1(csdn yyds!);s1.resize(20, a);cout s1 endl; //输出: csdn yyds!aaaaaaaaaacout s1.size() endl;//输出20
}
6. size_t capacity() const
我们刚才提到 string 的底层维护了一个 capacity 用来表示字符数组的实际大小这个函数就是用来返回这个 capacity 的。此函数用得也不多
不同编译器对于 string 维护的数组开空间的规则不同因此返回相同字符串的capacity不同编译器得到的值也不一定相同。下面是VS2022的结果
#includeiostream
#includestring
using namespace std;int main()
{string s;cout s.capacity() endl; //输出15
}
7: void reserve (size_t n 0)
这个函数是用来改变 string 维护的字符数组的实际容量的。 8. void clear()
清空一个字符串的内容使之变为空串 9. bool empty() const
判断一个字符串是不是空串
10. operator[]
operator[] 能够使得字符串支持下标访问这个是平时用得非常多的他提供了两个版本一个是非const版本一个是const版本。const 修饰的string对象通过下标访问得到的字符不允许修改。而非const的string对象通过下标访问得到的字符可以修改。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello world);s1[5] #;cout s1 endl; //输出hello#world
}
const修饰的string对象不可修改 11. char at(size_t pos)
此函数完全可以被operator[]代替哈at函数返回的是下标为 pos 的字符。 同样也有const版本和非const版本。
12. char back()
该函数返回字符串的最后一个字符同样也有 const 和非const 两个版本。
const版本const char back() const
非const版本char back()
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello world);char tail s1.back(); //非const支持修改tail C;cout s1 endl;//输出hello worlC
}
13. char front()
该函数返回字符串的第一个字符同样有 const 和非 const 两个版本。
const char front() const
char front()
14. operator
string 重载了 运算符支持一个字符串加等一个字符一个字符串常量一个string 对象。
#includeiostream
#includestring
using namespace std;int main()
{string s(csdn yyds!);s a;cout s endl; //输出csdn yyds!as bbb;cout s endl; //输出csdn yyds!abbbstring tmp(ccc);s tmp;cout s endl; //输出csdn yyds!abbbccc
}
15. string append (const char* s)
往一个string对象的末尾追加一个 常量字符串 或者 数组。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello );s1.append(world);cout s1 endl; //输出hello world
}
我们查阅 资料 发现 append 重载了很多版是不是觉得string的设计有点冗余。个人感觉最好用的还是operator。 16. void push_back (char c) 往string后面追加一个字符只能说非常鸡肋。不如operator。
17. insert()
我们可以看到 insert 重载了巨多版本真令人头大呢我们就讲讲两个具有代表性的。其他的都大同小异。 17.1 string insert (size_t pos, const char* s) 该函数用于在pos位置插入一个 常量字符串 或者 字符数组(字符数组一定要有 \0 哦)。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);s1.insert(5, hi); //在下标为5的位置插入 hicout s1 endl; //输出hello hi csdn}
17.2 string insert (size_t pos, size_t n, char c)
该函数用于在pos位置插入n个相同的字符。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);s1.insert(5, 5, 1); //在下标为5的位置插入5个1cout s1 endl; //输出hello11111 csdn}
18. string erase (size_t pos 0, size_t len npos)
这个函数可以将从pos位置开始后面的len个字符全部删除不传len表示len很大也就是移除pos后面的所有字符啦
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);s1.erase(0, 6); //移除下标 0 后面的6个字符cout s1 endl; //输出csdnstring s2(hello csdn);s2.erase(1); //移除下标1后的所有字符cout s2 endl; //输出h}
19. void pop_back() 这个函数简单删除string的最后那个字符。
20. const char* c_str() const
这个函数可以返回 C风格的字符串。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);const char* s s1.c_str();cout s endl; //输出hello csdnreturn 0;
}
C风格的字符串char类型的数组每个下标对应一个字符末尾有一个 \0。 21. find()
find 重载的版本也是比较多的呢老规矩讲常用的 21.1 size_t find (const char* s, size_t pos 0) const 这个函数用来在一个string中从pos位置开始查找一个常量字符串。pos不传的话就是从头查找。如果查找到了返回起始下标如果没有查找到返回 npos 。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);//从下标为1的位置开始查找 csdn 这个常量字符串cout s1.find(csdn, 1) endl; //输出6//从下标为7的位置开始查找 csdn 这个常量字符串cout s1.find(csdn, 7) endl; //输出4294967295(这是无符号整形的最大值)cout (int)s1.find(csdn, 7) endl; //输出-1}
21. 2 size_t find (char c, size_t pos 0) const
这个函数用来在一个string中从pos位置开始查找字符 c。pos不传就是从头开始查找。一旦查找成功 (即从pos位置开始第一个字符 c )返回查找成功的下标否则返回 npos。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);//从下标为 1 的位置开始查找字符lcout s1.find(l, 1) endl; //输出2//从下标为 4 的位置开始查找字符lcout s1.find(l, 4) endl; //输出4294967295(这是无符号整形的最大值)cout (int)s1.find(l, 4) endl; //输出-1
}
22. rfind()
我们可以看到参数的形式跟find完全一样rfind只是从后往前找罢了 22.1 size_t rfind (const char* s, size_t pos npos) const
表示在 string 对象中从pos位置往前查找常量字符串 s 。pos不传就是从末尾向前查找。查找成功返回起始位置的下标查找失败返回 npos。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);//从下标为 8 的位置往前查找常量字符串 csdncout s1.rfind(csdn, 8) endl; //输出6//从下标为 5 的位置往前查找常量字符串 csdncout s1.rfind(csdn, 5) endl; //输出4294967295(这是无符号整形的最大值)cout (int)s1.rfind(csdn, 5); //输出-1}
22.2 size_t rfind (char c, size_t pos npos) const
表示从pos位置开始向前查找字符 c 。pos不传就是从末尾向前查找。一旦查找成功 (pos位置往前第一个字符c的下标) 返回该下标否则返回 npos。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);//从下标为 8 的位置开始向前查找字符ccout s1.rfind(c, 8) endl; //输出6//从下标为 5 的位置开始向前查找字符ccout s1.rfind(c, 5) endl; //输出4294967295(这是无符号整形的最大值)cout (int)s1.rfind(c, 5) endl; //输出-1
}
23. string substr (size_t pos 0, size_t len npos) const 这是用截取原string对象中的一部分连续字符返回一个新的string对象。简言之获取子串。
表示从 pos 位置开始截取 len 个长度的字符来构造新的string。pos不传默认从头开始截取。len不传默认截取到末尾。
#includeiostream
#includestring
using namespace std;int main()
{string s1(hello csdn);//不传len默认截取到末尾从 6 截取到末尾cout s1.substr(6) endl; //输出csdn//从 0 开始截取 5 个字符cout s1.substr(0, 5) endl; //输出 hello
} 总结不需要死记硬背用得多了自然就记得了string的接口真的很多记不起来查查文档就知道了 string - C Reference (cplusplus.com)