C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++获取字符串长度详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
String 类型对象包括三种求解字符串长度的函数size()length()、 @H_10_0@maxsize() 和 capacity()
  • size() 和 length():这两个函数会返回 String 类型对象中的字符个数,且它们的执行效果相同。
  • max_size():max_size() 函数返回 String 类型对象最多包含的字符数。一旦程序使用长度超过 max_size() 的 String 操作,编译器会拋出 length_error 异常。
  • capacity():该函数返回在重新分配内存之前,String 类型对象所能包含的最大字符数。

String 类型对象还包括一个 reserve() 函数调用函数可以为 String 类型对象重新分配内存。重新分配的大小由其参数决定。reserve() 的认参数为 0。

上述几个函数的使用方法如下程序所示:
#include <iostream>
#include <String>
using namespace std;
int main ()
{
    int size = 0;
    int length = 0;
    unsigned long maxsize = 0;
    int capacity=0;
    String str ("12345678");
    String str_custom;
    str_custom = str;
    str_custom.resize (5);
    size = str_custom.size();
    length = str_custom.length();
    maxsize = str_custom.max_size();
    capacity = str_custom.capacity();
    cout << "size = " << size << endl;
    cout << "length = " << length << endl;
    cout << "maxsize = " << maxsize << endl;
    cout << "capacity = " << capacity << endl;
    return 0;
}
程序执行结果为:

size = 8
length = 8
maxsize = 2147483647
capacity = 15

由此程序可知String 类型对象 str_custom 调用 reserve() 函数时,似乎并没有起到重新分配内存的目的(笔者所用编译器为 Visual C++6.0)。

修改上述代码删除语句 str_custom.reserve (5),在代码 str_custom = str 之后如下添加代码

str_custom.resize (5);

修改后程序的执行结构如下:

size = 5
length = 5
maxsize = 2147483647
capacity = 15

重新设置 String 类型对象 str_custom 的大小之后,重新求解 str_custom 的大小,其执行效果与设置的数值一致(均为 5)。

大佬总结

以上是大佬教程为你收集整理的C++获取字符串长度详解全部内容,希望文章能够帮你解决C++获取字符串长度详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: